C ++ memory_order_consume,kill_dependency,dependency-ordered-before,synchronizes-with [英] C++ memory_order_consume, kill_dependency, dependency-ordered-before, synchronizes-with

查看:416
本文介绍了C ++ memory_order_consume,kill_dependency,dependency-ordered-before,synchronizes-with的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Anthony Williams的 C ++并行操作

I am reading C++ Concurrency in Action by Anthony Williams. Currently I at point where he desribes memory_order_consume.

在此块之后是:


现在我已经介绍了内存排序的基本知识,现在是时候看一下
更复杂的部分了。

Now that I’ve covered the basics of the memory orderings, it’s time to look at the more complex parts

它让我害怕一点,因为我不完全理解几个事情:

It scares me a little bit, because I don't fully understand several things:

不同于同步?他们都创造了发生之前的关系。有什么区别?

How dependency-ordered-before differs from synchronizes-with? They both create happens-before relationship. What is exact difference?

我对以下示例感到困惑:

I am confused about following example:

int global_data[]={ … };
std::atomic<int> index;
void f()
{
    int i=index.load(std::memory_order_consume);
    do_something_with(global_data[std::kill_dependency(i)]);
}

kill_dependency究竟做了什么?它杀死了哪个依赖?在哪些实体之间?

What does kill_dependency exactly do? Which dependency it kills? Between which entities? And how compiler can exploit that knowladge?

memory_order_consume的所有出现都可以被memory_order_acquire安全地替换?也就是说

Can all occurancies of memory_order_consume be safely replaced with memory_order_acquire? I.e. is it stricter in all senses?

在清单5.9中,我可以安全地替换

At Listing 5.9, can I safely replace

std::atomic<int> data[5]; // all accesses are relaxed

int data[5]

?也就是说可以获取和释放用于同步访问非原子数据?

? I.e. can acquire and release be used to synchronize access to non-atomic data?

他描述了放松,获取和释放例子与在小卧室的人。对于seq_cst和consume有一些类似的简单描述吗?

He describes relaxed, acquire and release by some examples with mans in cubicles. Are there some similar simple descriptions of seq_cst and consume?

推荐答案

说明。当多个线程访问相同的数据时,有三件事情可能会出错:

As to the next to last question, the answer takes a little more explanation. There are three things that can go wrong when multiple threads access the same data:


  1. 系统可能会在

  1. the system might switch threads in the middle of a read or write, producing a result that's half one value and half another.

编译器可能会移动代码,假设没有其他线程查看所涉及的数据。

the compiler might move code around, on the assumption that there is no other thread looking at the data that's involved.

处理器可能在其本地缓存中保留一个值,而不更改主存储器或重新读取它

the processor may be keeping a value in its local cache, without updating main memory after changing the value or re-reading it after another thread changed the value in main memory.

内存顺序地址 。原子函数地址1和2,以及取决于存储器顺序参数,也可以是3。所以memory_order_relaxed意味着不要打扰数字3.代码仍然处理1和2.在这种情况下,你将使用获取和释放,以确保正确的内存排序。

Memory order addresses only number 3. The atomic functions address 1 and 2, and, depending on the memory order argument, maybe 3 as well. So memory_order_relaxed means "don't bother with number 3. The code still handles 1 and 2. In that case, you'd use acquire and release to ensure proper memory ordering.

这篇关于C ++ memory_order_consume,kill_dependency,dependency-ordered-before,synchronizes-with的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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