在C / C ++,是volatile变量保证有最终一致的语义悫线程? [英] In C/C++, are volatile variables guaranteed to have eventually consistent semantics betwen threads?

查看:101
本文介绍了在C / C ++,是volatile变量保证有最终一致的语义悫线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有任何普遍遵循标准的任何保证(ISO C或C ++或
任何POSIX / SUS规格)一个变量(也许
标记为volatile),而不是由一个互斥锁保护,被访问
被多个线程将成为最终一致,如果它是
分配?

Is there any guarantee by any commonly followed standard (ISO C or C++, or any of the POSIX/SUS specifications) that a variable (perhaps marked volatile), not guarded by a mutex, that is being accessed by multiple threads will become eventually consistent if it is assigned to?

要提供一个具体的例子,考虑两个线程共享一个
变量v,与初始值为零。

To provide a specific example, consider two threads sharing a variable v, with initial value zero.

主题1:
  V = 1

Thread 1: v = 1

主题2:
  而(V == 0)
    收益率();

Thread 2: while(v == 0) yield();

是线程2确保最终能够终止?还是可以
可以想象旋永远,因为高速缓存一致性从来没有踢
并使得分配在线程2的缓存可见?

Is thread 2 guaranteed to terminate eventually? Or can it conceivably spin forever because the cache coherency never kicks in and makes the assignment visible in thread 2's cache?

我知道了C和C ++标准(C ++ 0x中之前)根本不说话
所有关于线程或并发性。不过我很好奇,如果C ++ 0x中
内存模型,或pthreads的,或其他任何东西,这保证。
(显然,这实际上不会在Windows 32位x86工作;我想知道如果它的东西,通常可以依靠,或者如果它只是碰巧在那里工作)。

I'm aware the C and C++ standards (before C++0x) do not speak at all about threads or concurrency. But I'm curious if the C++0x memory model, or pthreads, or anything else, guarantees this. (Apparently this does actually work on Windows on 32-bit x86; I'm wondering if it's something that can be relied on generally or if it just happens to work there).

推荐答案

这将取决于你的架构。虽然这是不寻常的需要一个明确的缓存刷新或内存同步,以确保内存写入是可见的其他线程,没有什么precludes它,我当然遇到平台(包括我目前正在开发的基于PowerPC的设备)其中,明确指示必须执行,以确保状态被刷新。

It's going to depend on your architecture. While it is unusual to require an explicit cache flush or memory sync to ensure memory writes are visible to other threads, nothing precludes it, and I've certainly encountered platforms (including the PowerPC-based device I am currently developing for) where explicit instructions have to be executed to ensure state is flushed.

请注意该线程同步原语像互斥的要求将执行必要的工作,但你通常不会真的需要一个线程同步原语,如果你想要的是确保国家是可见的,而无需关心一致性 - 只是同步/清洗指令就足够了。

Note that thread synchronisation primitives like mutexes will perform the necessary work as required, but you don't typically actually need a thread synchronisation primitive if all you want is to ensure the state is visible without caring about consistency - just the sync / flush instruction will suffice.

编辑:现在还有人在有关挥发性关键字confustion - 挥发性保证编译器不会产生code明确在缓存寄存器中的数据,但是这是不会同样的事情处理的透明缓存/重新排序的读取和写入硬件。例如阅读<一href=\"http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/\">this或或的这个的Dr Dobbs的文章,或者答案<一个href=\"http://stackoverflow.com/questions/1787450/how-do-i-understand-read-memory-barriers-and-volatile\">this SO问题,或者只挑自己喜欢的编译器,针对一个弱一致内存架构样细胞,编写一些测试code和比较,编译器产生什么你才能需要什么,以确保写是可见的其他进程。

To anyone still in confustion about the volatile keyword - volatile guarantees the compiler will not generate code that explicitly caches data in registers, but this is NOT the same thing as dealing with hardware that transparently caches / reorders reads and writes. Read e.g. this or this, or this Dr Dobbs article, or the answer to this SO question, or just pick your favourite compiler that targets a weakly consistent memory architecture like Cell, write some test code and compare what the compiler generates to what you'd need in order to ensure writes are visible to other processes.

这篇关于在C / C ++,是volatile变量保证有最终一致的语义悫线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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