gpt4 book ai didi

c++ - 为什么 memory_order_relaxed 和 memory_order_seq_cst 没有区别?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:46 25 4
gpt4 key购买 nike

我正在玩 C++ Concurrency in Action 中的一个示例,它使用 std::memory_order_relaxed 从 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;
}

每次运行该程序时,我都会得到完全相同的输出:

(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)

如果我从 std::memory_order_relaxed 更改为 std::memory_order_seq_cst,程序会给出完全相同的输出!

我本以为程序的 2 个版本会有不同的输出。为什么 std::memory_order_relaxedstd::memory_order_seq_cst 的输出没有区别?

为什么 std::memory_order_relaxed 每次运行程序总是产生完全相同的结果?

我正在使用: - 作为虚拟机安装的 32 位 Ubuntu(在 VMWare 下) - 英特尔四核处理器 - 海湾合作委员会 4.6.1-9

代码编译为: g++ --std=c++0x -g mem-order-relaxed.cpp -o relaxed -pthread

注意-pthread是必须的,否则报如下错误: 在抛出“std::system_error”的实例后调用终止 what(): 不允许操作

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

最佳答案

您为 VM 分配了多少个处理器内核?为 VM 分配多个核心以使其利用并发性。

关于c++ - 为什么 memory_order_relaxed 和 memory_order_seq_cst 没有区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9886246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com