当前位置:网站首页>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.
边栏推荐
- win10为任意文件添加右键菜单
- Markem imaje马肯依玛士喷码机维修9450E打码机维修
- Rough reading DS transunet: dual swing transformer u-net for medical image segmentation
- oracle怎样将字符串转为多行
- Installation and usage of source insight tool
- prometheus告警流程及相关时间参数说明
- The difference between ArrayList and LinkedList
- Summary of three basic interview questions
- 手把手带你玩摄像头模组
- Quelques exercices sur les arbres binaires
猜你喜欢
Win10 add right-click menu for any file
E+h secondary meter repair pH transmitter secondary display repair cpm253-mr0005
Quelques exercices sur les arbres binaires
Flow chart of Alipay wechat payment business
June 26, 2022 (LC 6100 counts the number of ways to place houses)
Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher
最全H桥电机驱动模块L298N原理及应用
多网络设备存在时,如何配置其上网优先级?
Redis master-slave replication and sentinel mode
高等数学第七章微分方程
随机推荐
力扣84柱状图中最大的矩形
IMX8QXP DMA资源和使用(未完结)
ucore lab5
Semi-supervised Learning入门学习——Π-Model、Temporal Ensembling、Mean Teacher简介
Object含有Copy方法?
最全H桥电机驱动模块L298N原理及应用
多个类的设计
MATLAB小技巧(18)矩阵分析--熵权法
Matlab tips (19) matrix analysis -- principal component analysis
直接修改/etc/crontab 文件内容,定时任务不生效
内存泄露的最直接表现
fastadmin 安装后访问后台提示模块不存在
Redis transactions
The most direct manifestation of memory leak
微信小程序学习之五种页面跳转方法.
Installation and use of SVN version controller
1098 Insertion or Heap Sort(堆排序解释)(PAT甲级)
This, constructor, static, and inter call must be understood!
VIM from dislike to dependence (19) -- substitution
Parameters argc and argv of main()