没有内存屏障的无序写入:数据竞争的唯一可能的原因? [英] Out of order writes without memory-barrier: the only possible cause of Data Race?

查看:234
本文介绍了没有内存屏障的无序写入:数据竞争的唯一可能的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实际操作中,由Brian Goetz实施Java并发时遇到以下行:

While going through Java Concurrency in practice by Brian Goetz I encountered the following line:


当变量被多个线程
读取并且由至少一个线程写入时,发生变化,但是读取和写入不是通过happens-before 排序的
。一个正确同步的程序是一个
没有数据竞争;正确同步的程序表现出顺序的
一致性,意味着程序中的所有动作似乎都以固定的全局顺序出现。

A data race occurs when a variable is read by more than one thread, and written by at least one thread, but the reads and writes are not ordered by happens-before. A correctly synchronized program is one with no data races; correctly synchronized programs exhibit sequential consistency, meaning that all actions within the program appear to happen in a fixed, global order.

我的问题是,是不是顺序写的是数据竞争条件在java或可能在其他编程语言的唯一原因?

UPDATE

好​​的,我做了一些关于数据竞赛的调查,发现从 oracle官方网站,它说:

My Question is that, Is Out of Order writes the only reason for Data Race condition in java or possibly in other programming languages?
UPDATE
OK, I did some more investigation about data-race and found the following from oracle official site which says that :


Thread Analyzer检测执行
的多线程进程。数据竞争发生在以下时间:

The Thread Analyzer detects data-races that occur during the execution of a multi-threaded process. A data race occurs when:


  • 单个进程中的两个或多个线程同时访问同一个内存位置,

  • 至少有一个访问用于写入,

  • 线程未使用任何排他锁来控制对该内存的访问。

当这三个条件成立时,访问顺序是
非确定性
,计算可能会给出不同的结果
根据该顺序运行运行。一些数据竞争可能是良性的(对于
示例,当存储器访问用于忙等待时),但是许多
数据竞争是程序中的错误。

When these three conditions hold, the order of accesses is non-deterministic, and the computation may give different results from run to run depending on that order. Some data-races may be benign (for example, when the memory access is used for a busy-wait), but many data-races are bugs in the program.


在本部分中,提到:访问顺序是非确定性的

它是谈论Threads访问内存位置的顺序吗?如果是,那么同步永远不能保证线程将访问代码块的顺序。那么,同步如何解决数据竞争的问题?



In this part, it is mentioning that : the order of accesses is non-deterministic
Is it talking about the the sequence in which Threads are accessing the memory location? If yes, then synchronization never guarantee about the order in which threads will access the block of code. So , how synchronization can resolve the issue of data race?

推荐答案

我宁愿将数据竞争定义为


从某个变量读取某个值或引用的数据竞争是一种情况,当读取结果由内部 (jvm或os控制)线程调度。

Data race between writing and reading of some value or reference from a variable is a situation when the result of reading is determined by the "internal" (jvm- or os-controlled) thread scheduling.

事实上,问题的第二个定义说 ):

In fact, second definition from the question says the same in more "official" words :)

换句话说,考虑线程A向变量写入一些值,线程B试图读取它。如果您错过任何类型的同步(或其他可以在写入和后续读取之间提供发生前保证的机制),则您的程序在线程A和线程B之间有数据竞争。

In the other words, consider thread A writing some value to the variable and thread B attempting to read it. If you miss any kind of synchronization (or other mechanism that can provide happens-before guarantees between write and subsequent read), your program has a data race between threads A and B.

现在,对你的问题:


它是谈论Threads访问的顺序内存位置?如果是,则同步永远不能保证线程将访问代码块的顺序。

Is it talking about the the sequence in which Threads are accessing the memory location? If yes, then synchronization never guarantee about the order in which threads will access the block of code.

你将永远无法读取该变量在写线程退出 synchronized 块或方法之后写入新值之前的值。没有同步化,即使在写入实际发生之后,也有机会读取旧的值。

Synchronization in that particular case guarantees that you will never be able to read value that variable had before the writer thread written new value after writer thread exited synchronized block or method. Without syncronization, there is a chance to read old value even after write is actually happened.

关于访问顺序:它将是确定性的,方式:

About the order of access: it is going to be deterministic with synchronization in the following way:

让我们再次看看我们的线程A和B.操作顺序现在是顺序的 - 线程B将不能开始读取,直到线程A完成写入。为了使这种情况清楚,想象写作和阅读是一个漫长的过程。没有同步,这些操作将能够相互交错,这可能导致读取一些无意义的值。

Let's take a look at our threads A and B again. The operations order is now sequential - thread B will not be able to start reading until thread A finished with writing. To get this situation clear, imagine that writing and reading is really a long process. Without synchronization, these operations will be able to interlap with each other which might result in some meaningless values read.

这篇关于没有内存屏障的无序写入:数据竞争的唯一可能的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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