线程安全编程 [英] Thread safe programming

查看:137
本文介绍了线程安全编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持听到线程安全。

I keep hearing about thread safe. What is that exactly and how and where can I learn to program thread safe code?

另外,假设我有2个线程,一个写入结构体,另一个写入结构体读取它。这是危险的任何方式吗?有什么我应该寻找吗?我不认为这是一个问题。这两个线程不会(也不能)在同一时间访问结构。

Also, assume I have 2 threads, one that writes to a structure and another one that reads from it. Is that dangerous in any way? Is there anything I should look for? I don't think it is a problem. Both threads will not (well can't ) be accessing the struct at the exact same time..

另外,有人可以告诉我在这个例子中: http://stackoverflow.com/a/5125493/1248779 我们正在做一个更好的并发问题。

Also, can someone please tell me how in this example : http://stackoverflow.com/a/5125493/1248779 we are doing a better job in concurrency issues. I don't get it.

推荐答案

线程安全是一个更大的问题集的一个方面,并发编程。我建议阅读这个主题。

Thread-safety is one aspect of a larger set of issues under the general heading of "Concurrent Programming". I'd suggest reading around that subject.

你假设两个线程不能同时访问结构体是不好的。首先:今天我们有多核机器,所以两个线程可以在完全相同的时间运行。第二:即使在单个核心机器上,给予任何其他线程的时间片是不可预测的。你必须预期蚂蚁任何任意时间的其他线程可能正在处理。请参阅我的机会之窗示例。

Your assumption that two threads cannot access the struct at the same time is not good. First: today we have multi-core machines, so two threads can be running at exactly the same time. Second: even on a single core machine the slices of time given to any other thread are unpredicatable. You have to anticipate that ant any arbitrary time the "other" thread might be processing. See my "window of opportunity" example below.

线程安全的概念正是为了回答这是危险的任何方式的问题。关键问题是,在一个线程中运行的代码是否可能得到一些数据的不一致视图,这种不一致性发生,因为当它运行另一个线程时,正在改变数据。

The concept of thread-safety is exactly to answer the question "is this dangerous in any way". The key question is whether it's possible for code running in one thread to get an inconsistent view of some data, that inconsistency happening because while it was running another thread was in the middle of changing data.

在你的示例中,一个线程正在读取一个结构,同时另一个线程正在写入。假设有两个相关字段:

In your example, one thread is reading a structure and at the same time another is writing. Suppose that there are two related fields:

  { foreground: red; background: black }

,并且作者正在更改这些

and the writer is in the process of changing those

   foreground = black;
            <=== window of opportunity
   background = red;

如果读者只读取这个机会的值,那么它会看到一个废话 / p>

If the reader reads the values at just that window of opportunity then it sees a "nonsense" combination

  { foreground: black; background: black }

这种模式的本质是短暂的时间,更改,系统变得不一致,读者不应该使用值。

This essence of this pattern is that for a brief time, while we are making a change, the system becomes inconsistent and readers should not use the values. As soon as we finish our changes it becomes safe to read again.

因此,我们使用Stefan提到的CriticalSection API来防止线程看到不一致的状态。

Hence we use the CriticalSection APIs mentioned by Stefan to prevent a thread seeing an inconsistent state.

这篇关于线程安全编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆