为什么memory_order_relaxed和memory_order_seq_cst没有区别? [英] Why does memory_order_relaxed and memory_order_seq_cst make no difference?

查看:149
本文介绍了为什么memory_order_relaxed和memory_order_seq_cst没有区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩《 C ++并发性行动》中的一个示例,该示例使用std::memory_order_relaxed读取和写入来自5个不同线程的3个原子变量.示例程序如下:

I was playing with one of the examples in C++ Concurrency in Action which uses std::memory_order_relaxed for reading and writing 3 atomic variables from 5 different threads. The example program is as follows:

#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;
}

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

Every time I run the program I get exactly the same output:

(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,该程序将给出完全相同的输出!

If I change from std::memory_order_relaxed to std::memory_order_seq_cst the program gives exactly the same output!

我期望该程序的2个版本具有不同的输出.为什么std::memory_order_relaxedstd::memory_order_seq_cst的输出之间没有区别?

I would have expected different output from the 2 versions of the program. Why is there no difference between the output for std::memory_order_relaxed and std::memory_order_seq_cst?

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

Why does std::memory_order_relaxed always produce exactly the same results for every run of the program?

我正在使用: -将32位Ubuntu安装为虚拟机(在VMWare下) -Intel四核处理器 -GCC 4.6.1-9

I am using: - 32bit Ubuntu installed as a virtual machine (under VMWare) - An INtel Quad Core processor - GCC 4.6.1-9

使用以下代码编译代码: g ++ --std = c ++ 0x -g mem-order-relaxed.cpp -o Relaxed -pthread

The code is compiled with: g++ --std=c++0x -g mem-order-relaxed.cpp -o relaxed -pthread

请注意,-pthread是必需的,否则将报告以下错误: 抛出'std :: system_error'实例后终止调用 what():不允许操作

Note the -pthread is necessary, otherwise the following error is reported: terminate called after throwing an instance of 'std::system_error' what(): Operation not permitted

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

Is the behaviour I am seeing due to lack of support with GCC, or as a result of running under VMWare?

推荐答案

您已为VM分配了多少个处理器核心?为VM分配多个内核,以利用并发性.

How many processor cores do you have assigned to the VM? Assign multiple cores to the VM to let it take advantage of concurrency.

这篇关于为什么memory_order_relaxed和memory_order_seq_cst没有区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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