非原子对象在所有线程中都具有相同的修改顺序吗? (没有数据竞赛)

时间:2022-04-18 21:01:02

1.10/6:

1.10 / 6:

All modifications to a particular atomic object M occur in some particular total order, called the modification order of M.

对特定原子对象M的所有修改都以某种特定的总顺序出现,称为M的修改顺序。

Do non-atomic objects also have same modification order in all threads? I am interested in properly synchronized cases (in absence of data races, etc).

非原子对象在所有线程中也具有相同的修改顺序吗?我对正确同步的情况感兴趣(没有数据竞赛等)。

1 个解决方案

#1


4  

You need to have synchronization between all participating threads.

您需要在所有参与的线程之间进行同步。

If you have one (or more) thread(s) updating a non-atomic value (with any amount of synchronizing operations among their group) and another thread reading that value (without synchronizing with any updater threads), you are not even guaranteed that you will read one of the values the other threads have stored.

如果你有一个(或多个)线程更新非原子值(在其组之间有任意数量的同步操作)和另一个线程读取该值(不与任何更新程序线程同步),你甚至不能保证您将读取其他线程存储的值之一。

Generally, if one thread updates a non-atomic variable and another accesses (updates or reads) it without proper synchronization between these two operations, you have a data race. A data race causes undefined behavior.

通常,如果一个线程更新非原子变量而另一个线程在没有这两个操作之间正确同步的情况下访问(更新或读取)它,则会产生数据争用。数据争用导致未定义的行为。

If you "properly synchronize" accesses to a non-atomic variable, you will have a happens-before relationship between any modification and any access in another (or of course the same) thread. This includes the relationship between any two modifications.

如果您“正确地同步”对非原子变量的访问,那么您将在任何修改与另一个(或当然是相同的)线程中的任何访问之间建立先发生关系。这包括任何两个修改之间的关系。

That happens-before relationship is valid across all threads and induces a total order between modifications. So, yes: properly synchronized use gives you a total modification order (which even is the same for all variables synchronized using the same synchronizing operations).

这种情况发生在关系在所有线程中有效并在修改之间产生总顺序之前。所以,是的:正确同步使用会给你一个完整的修改顺序(对于使用相同的同步操作同步的所有变量,它甚至是相同的)。

#1


4  

You need to have synchronization between all participating threads.

您需要在所有参与的线程之间进行同步。

If you have one (or more) thread(s) updating a non-atomic value (with any amount of synchronizing operations among their group) and another thread reading that value (without synchronizing with any updater threads), you are not even guaranteed that you will read one of the values the other threads have stored.

如果你有一个(或多个)线程更新非原子值(在其组之间有任意数量的同步操作)和另一个线程读取该值(不与任何更新程序线程同步),你甚至不能保证您将读取其他线程存储的值之一。

Generally, if one thread updates a non-atomic variable and another accesses (updates or reads) it without proper synchronization between these two operations, you have a data race. A data race causes undefined behavior.

通常,如果一个线程更新非原子变量而另一个线程在没有这两个操作之间正确同步的情况下访问(更新或读取)它,则会产生数据争用。数据争用导致未定义的行为。

If you "properly synchronize" accesses to a non-atomic variable, you will have a happens-before relationship between any modification and any access in another (or of course the same) thread. This includes the relationship between any two modifications.

如果您“正确地同步”对非原子变量的访问,那么您将在任何修改与另一个(或当然是相同的)线程中的任何访问之间建立先发生关系。这包括任何两个修改之间的关系。

That happens-before relationship is valid across all threads and induces a total order between modifications. So, yes: properly synchronized use gives you a total modification order (which even is the same for all variables synchronized using the same synchronizing operations).

这种情况发生在关系在所有线程中有效并在修改之间产生总顺序之前。所以,是的:正确同步使用会给你一个完整的修改顺序(对于使用相同的同步操作同步的所有变量,它甚至是相同的)。