当前位置:网站首页>std::memory_ order_ seq_ CST memory order
std::memory_ order_ seq_ CST memory order
2022-06-27 09:20:00 【wangzai6378】
One 、 In the case of multiple producers and consumers
#include <thread>
#include <atomic>
#include <cassert>
std::atomic<bool> x = {false};
std::atomic<bool> y = {false};
std::atomic<int> z = {0};
void write_x()
{
x.store(true, std::memory_order_seq_cst);
}
void write_y()
{
y.store(true, std::memory_order_seq_cst);
}
void read_x_then_y()
{
while (!x.load(std::memory_order_seq_cst))
;
if (y.load(std::memory_order_seq_cst)) {
++z;
}
}
void read_y_then_x()
{
while (!y.load(std::memory_order_seq_cst))
;
if (x.load(std::memory_order_seq_cst)) {
++z;
}
}
int main()
{
std::thread a(write_x);
std::thread b(write_y);
std::thread c(read_x_then_y);
std::thread d(read_y_then_x);
a.join(); b.join(); c.join(); d.join();
assert(z.load() != 0); // will never happen
}Threads a and b producers , Threads c and d Is the consumer . All producers (a and b) Memory sorting occurs ; All consumers (c and d) At the time of consumption , The code is executed in the memory sequence arranged for production . therefore , here c and d Observe a and b when , Or first x Turn into true, then y Turn into true; Or first y Turn into true, Again x Turn into true. In either case at least 1.
边栏推荐
- Fake constructor???
- i=i++;
- 不容置疑,这是一个绝对精心制作的项目
- That is, a one-stop live broadcast service with "smooth live broadcast" and full link upgrade
- Design of multiple classes
- 1098 Insertion or Heap Sort(堆排序解释)(PAT甲级)
- 我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
- [MySQL basic] general syntax 1
- IO pin configuration and pinctrl drive
- Nosql 数据库 -Redis 安装
猜你喜欢

SVN版本控制器的安装及使用方法

Take you to play with the camera module

ucore lab3
快捷键 bug,可复现(貌似 bug 才是需要的功能 [滑稽.gif])

Nosql 数据库 -Redis 安装

Rough reading DS transunet: dual swing transformer u-net for medical image segmentation
![[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy](/img/d6/119f32f73d25ddd97801f536d68752.png)
[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy

DV scroll board width of datav rotation table component

Apache POI的读写

Process 0, process 1, process 2
随机推荐
Rockermq message sending and consumption mode
MySQL proficient-01 addition, deletion and modification
The difference between ArrayList and LinkedList
ucore lab4
ServletConfig and ServletContext
The most direct manifestation of memory leak
数字IC-1.9 吃透通信协议中状态机的代码编写套路
三道基础面试题总结
Preliminary understanding of pytorch
使线程释放锁资源的操作/方法重载一点注意事项
Chapter 11 signal (I) - concept
Some exercises about binary tree
ucore lab3
静态代码块Vs构造代码块
今日3大面试Demo[Integer ASCII 类关系]
win10为任意文件添加右键菜单
Brief introduction to SSL encryption process
CLassLoader
Persistence mechanism of redis
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!