问题描述
这是一个复杂的问题,请在回答之前仔细考虑。
This is a complex question, please consider carefully before answering.
考虑这种情况。两个线程(读者和作家)访问一个单一的全球 INT
。这安全吗?通常情况下,我会不假思索回应,是的!
Consider this situation. Two threads (a reader and a writer) access a single global int
. Is this safe? Normally, I would respond without thought, yes!
不过,在我看来,香草萨特并不这么认为。在他的有效的并发性的文章,他讨论了和的。
However, it seems to me that Herb Sutter doesn't think so. In his articles on effective concurrency he discusses a flawed lock-free queue and the corrected version.
在第一篇文章中,他讨论的变量很少考虑特质第二的开头结尾,写序。诠释的是原子的,不错,但整数不一定订购了可以摧毁任何无锁的算法,包括我上面的场景。我完全同意,唯一的办法的保证的正确的多线程在所有平台上present和未来的行为是使用原子能公司(又名记忆障碍)或互斥。
In the end of the first article and the beginning of the second he discusses a rarely considered trait of variables, write ordering. Int's are atomic, good, but ints aren't necessarily ordered which could destroy any lock-free algorithm, including my above scenario. I fully agree that the only way to guarantee correct multithreaded behavior on all platforms present and future is to use atomics(AKA memory barriers) or mutexes.
我的问题;是写重odering曾经在真实的硬件出了问题?或者是多线程的偏执只是被迂腐?结果
什么经典的单处理器系统?结果
关于简单的RISC处理器就像一个嵌入式电源,PC是什么?
My question; is write re-odering ever a problem on real hardware? Or is the multithreaded paranoia just being pedantic?
What about classic uniprocessor systems?
What about simpler RISC processors like an embedded power-pc?
的澄清的:我更感兴趣的是萨特先生说一下硬件(处理器/缓存)重新排序变量写道。我可以从破code编译器与交换机或装配后编译的手检查停止优化。不过,我想知道如果硬件尚可乱了code在实践中。
Clarification: I'm more interested in what Mr. Sutter said about the hardware (processor/cache) reordering variable writes. I can stop the optimizer from breaking code with compiler switches or hand inspection of the assembly post-compilation. However, I'd like to know if the hardware can still mess up the code in practice.
推荐答案
您检查装配的想法是不够的;重排序可以在硬件级别发生
Your idea of inspecting the assembly is not good enough; the reordering can happen at the hardware level.
要回答你的问题,是这个不断在读取硬件出现了问题:是其实我碰到这个问题我自己
To answer your question "is this ever a problem on read hardware:" Yes! In fact I've run into that problem myself.
是否确定裙子单处理器系统或其他特殊情况的情况下,这个问题?我要说不,因为从现在起5年,你可能需要在多核毕竟运行,然后找到所有这些地点将是棘手的(不可能的?)。
Is it OK to skirt the issue with uniprocessor systems or other special-case situations? I would argue "no" because five years from now you might need to run on multi-core after all, and then finding all these locations will be tricky (impossible?).
一个例外:软件设计的嵌入式硬件应用中确实已经完全超过了硬件控制。其实我已经在例如这些情况下,骗像这样ARM处理器。
One exception: Software designed for embedded hardware applications where indeed you have completely control over the hardware. In fact I have "cheated" like this in those situations on e.g. an ARM processor.
这篇关于多线程的偏执狂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!