我想知道你是否有2个线程正在使用memory_order_acquire进行加载,而一个线程正在使用memory_acquire_release进行存储,那么加载只会与执行加载的两个线程中的一个同步?意思是它只能与存储/加载的一个线程同步,或者您可以让多个线程在执行存储的单个线程中执行同步加载。在研究它是一对一的关系之后,似乎就是这种情况,但是读 - 修改 - 写操作似乎是连锁的,见下文。
如果线程正在执行读取 - 修改 - 写入操作(如fetch_sub),那么它似乎工作得很好,即使在reader1_thread和reader2_thread之间没有同步关系,它们似乎也会有链接释放
std::atomic<int> s;
void loader_thread()
{
s.store(1,std::memory_order_release);
}
// seems to chain properly if these were fetch_sub instead of load,
// wondering if just loads will be synchronized as well meaning both threads
// will be synched up with the store in the loader thread or just the first one
void reader1_thread()
{
while(!s.load(std::memory_order_acquire));
}
void reader2_thread()
{
while(!s.load(std::memory_order_acquire));
}
答案 0 :(得分:3)
标准说“原子商店 - 版本与从商店获取其价值的加载获取同步”(C ++11§1.10[intro.multithread] p8)。值得注意的是不表示只能有一个这样的同步负载;确实任何原子加载 - 获取它从原子商店释放的值与该商店同步。