为什么记忆顺序放松和记忆顺序cst没有区别? 我在玩C++并发操作中的一个例子,它使用 STD::MeMyYyOrthOrthPosib从5个不同的线程读取和写入3个原子变量。示例程序如下所示: #include <thread> #include <atomic> #include <iostream> std::atomic<int> x(0); std::atomic<int> y(0); std::atomic<int> z(0); std::atomic<bool> go(false); const unsigned int loop_count = 10; struct read_values { int x; int y; int z; }; read_values values1[loop_count]; read_values values2[loop_count]; read_values values3[loop_count]; read_values values4[loop_count]; read_values values5[loop_count]; void increment( std::atomic<int>* v, read_values* values ) { while (!go) std::this_thread::yield(); for (unsigned i=0;i<loop_count;++i) { values[i].x=x.load( std::memory_order_relaxed ); values[i].y=y.load( std::memory_order_relaxed ); values[i].z=z.load( std::memory_order_relaxed ); v->store( i+1, std::memory_order_relaxed ); std::this_thread::yield(); } } void read_vals( read_values* values ) { while (!go) std::this_thread::yield(); for (unsigned i=0;i<loop_count;++i) { values[i].x=x.load( std::memory_order_relaxed ); values[i].y=y.load( std::memory_order_relaxed ); values[i].z=z.load( std::memory_order_relaxed ); std::this_thread::yield(); } } void print( read_values* values ) { for (unsigned i=0;i<loop_count;++i) { if (i) std::cout << ","; std::cout << "(" << values[i].x <<"," << values[i].y <<"," << values[i].z <<")"; } std::cout << std::endl; } int main() { std::thread t1( increment, &x, values1); std::thread t2( increment, &y, values2); std::thread t3( increment, &z, values3); std::thread t4( read_vals, values4); std::thread t5( read_vals, values5); go = true; t5.join(); t4.join(); t3.join(); t2.join(); t1.join(); print( values1 ); print( values2 ); print( values3 ); print( values4 ); print( values5 ); return 0; }

为什么记忆顺序放松和记忆顺序cst没有区别? 我在玩C++并发操作中的一个例子,它使用 STD::MeMyYyOrthOrthPosib从5个不同的线程读取和写入3个原子变量。示例程序如下所示: #include <thread> #include <atomic> #include <iostream> std::atomic<int> x(0); std::atomic<int> y(0); std::atomic<int> z(0); std::atomic<bool> go(false); const unsigned int loop_count = 10; struct read_values { int x; int y; int z; }; read_values values1[loop_count]; read_values values2[loop_count]; read_values values3[loop_count]; read_values values4[loop_count]; read_values values5[loop_count]; void increment( std::atomic<int>* v, read_values* values ) { while (!go) std::this_thread::yield(); for (unsigned i=0;i<loop_count;++i) { values[i].x=x.load( std::memory_order_relaxed ); values[i].y=y.load( std::memory_order_relaxed ); values[i].z=z.load( std::memory_order_relaxed ); v->store( i+1, std::memory_order_relaxed ); std::this_thread::yield(); } } void read_vals( read_values* values ) { while (!go) std::this_thread::yield(); for (unsigned i=0;i<loop_count;++i) { values[i].x=x.load( std::memory_order_relaxed ); values[i].y=y.load( std::memory_order_relaxed ); values[i].z=z.load( std::memory_order_relaxed ); std::this_thread::yield(); } } void print( read_values* values ) { for (unsigned i=0;i<loop_count;++i) { if (i) std::cout << ","; std::cout << "(" << values[i].x <<"," << values[i].y <<"," << values[i].z <<")"; } std::cout << std::endl; } int main() { std::thread t1( increment, &x, values1); std::thread t2( increment, &y, values2); std::thread t3( increment, &z, values3); std::thread t4( read_vals, values4); std::thread t5( read_vals, values5); go = true; t5.join(); t4.join(); t3.join(); t2.join(); t1.join(); print( values1 ); print( values2 ); print( values3 ); print( values4 ); print( values5 ); return 0; },c++,concurrency,c++11,atomic,C++,Concurrency,C++11,Atomic,如果我从std::memory\u order\u released更改为std::memory\u order\u seq\u cst程序将给出完全相同的输出 我本来希望程序的两个版本有不同的输出。为什么std::memory\u order\u released和std::memory\u order\u seq\u cst的输出没有区别 为什么std::memory\u order\u released每次运行程序都会产生完全相同的结果 我正在使用: -32位Ubuntu作为虚拟机安装(在

如果我从
std::memory\u order\u released
更改为
std::memory\u order\u seq\u cst
程序将给出完全相同的输出

我本来希望程序的两个版本有不同的输出。为什么
std::memory\u order\u released
std::memory\u order\u seq\u cst
的输出没有区别

为什么
std::memory\u order\u released
每次运行程序都会产生完全相同的结果

我正在使用: -32位Ubuntu作为虚拟机安装(在VMWare下) -英特尔四核处理器 -通用条款4.6.1-9

该代码由以下代码编译: g++--std=c++0x-g mem-order-released.cpp-o released-pthread

注意-pthread是必需的,否则会报告以下错误: 在抛出“std::system_error”实例后调用terminate what():不允许操作


我看到的这种行为是由于缺乏对GCC的支持,还是在VMWare下运行的结果?

您为VM分配了多少个处理器内核?将多个内核分配给虚拟机,使其能够利用并发性。

您对收益率的使用导致您的程序的性能更依赖于平台的调度程序,而不是其他任何东西


也就是说,内存顺序并不要求编译器对原子重新排序,它只允许编译器这样做。如果编译器对它通过内存_order _seq _cst获得的顺序感到满意,那么它实际上可能会产生完全相同的字节码!在x86上尤其如此,因为指令集已经提供了许多排序保证,因此实现内存顺序并不是一个很大的飞跃。

许多版本的GCC忽略了您提供的内存顺序,并用顺序一致性替换它。您可以在头文件中看到这一点。希望他们最终能有一个更好的实现?您可以通过使用CDSChecker…

您为VM分配了多少个处理器内核?@Michael Burr-这就解决了-Vmware设置为1个内核,我刚刚将其设置为4个内核,现在我得到了预期的结果。不得不承认,我对使用VMWare还不熟悉——在工作中,我习惯将Linux作为本机操作系统。如果你发表你的建议,我会接受你的回答。干杯
(0,10,10),(1,10,10),(2,10,10),(3,10,10),(4,10,10),(5,10,10),(6,10,10),(7,10,10),(8,10,10),(9,10,10)
(0,0,1),(0,1,2),(0,2,3),(0,3,4),(0,4,5),(0,5,6),(0,6,7),(0,7,8),(0,8,9),(0,9,10)
(0,0,0),(0,1,1),(0,2,2),(0,3,3),(0,4,4),(0,5,5),(0,6,6),(0,7,7),(0,8,8),(0,9,9)
(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0)
(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0),(0,0,0)