Comments (4)
同时,即使是6,7两个线程交叉执行, 线程6中,x的赋值也是优于于y的赋值,那么x.load()也是true。
会执行z++的.
不知道我这个理解哪里有问题,谢谢
from cpp_concurrency_in_action.
Q:
为什么 5会被执行呢?
A:
显而易见,5肯定要执行。不管是6 7哪个线程先执行,你这个程序的顺序已经是固定死了的。这里的x,y类似于锁的功能。read_y_then_x中的while语句就是一个自旋锁,一直等到y被置于true时解锁。所以,read_y_then_x只能在write_x_then_y执行完成后,才能执行。这时x,y都是解锁的,也就是都是true,所以++z必然会执行。两个线程都执行完成了,join就会回收两个线程,所以assert(z.load()!=0);必然会执行。
Q:
再什么样的执行情况下,会满足z=0,
A:
你这个程序里面,程序结束后,z必然不为0。要想让z始终为0,那么只能让线程死锁,++z那步永远执行不到才行。
Q:
即使是6,7两个线程交叉执行, 线程6中,x的赋值也是优于于y的赋值,那么x.load()也是true。会执行z++的.
A:
在你给出的这个程序中,6 7其实并没有交叉运行,7在等6完成才能执行,因为7中while的自旋。
所以你想让始终z=0,你上面的程序是不可能实现的。
最后,你要想z=0,那就必然++z这个不能执行,你的程序简单改一下即可:
#include <atomic>
#include <thread>
#include <cassert>
std::atomic<bool> x, y;
std::atomic<int> z;
void read_y_then_x()
{
while (!y.load(std::memory_order_seq_cst)); // 3
if (!x.load(std::memory_order_seq_cst)) // 4
++z;
}
void write_x_then_y()
{
x.store(true, std::memory_order_relaxed); // 1
y.store(true, std::memory_order_relaxed); // 2
}
int main()
{
x = false;
y = false;
z = 0;
std::thread c(write_x_then_y); //6
std::thread d(read_y_then_x); //7
c.join();
d.join();
assert(z.load() != 0); // 5
}
from cpp_concurrency_in_action.
from cpp_concurrency_in_action.
@xiaoweiChen 您好
#include
#include
#include <assert.h>
std::atomic x,y;
std::atomic z;
void write_x_then_y()
{
x.store(true,std::memory_order_relaxed); // 1
y.store(true,std::memory_order_relaxed); // 2
}
void read_y_then_x()
{
while(!y.load(std::memory_order_relaxed)); // 3
if(x.load(std::memory_order_relaxed)) // 4
++z;
}
int main()
{
x=false;
y=false;
z=0;
std::thread a(write_x_then_y);
std::thread b(read_y_then_x);
a.join();
b.join();
assert(z.load()!=0); // 5
}
我再次看了一下你说的, 但是上面的那段代码是来至于Cpp_Concurrency_In_Action的第5章128页,用的是std::memory_order_relaxed 而非 std::memory_order_seq_cst.
我的本意是,看那段代码,很难看到z=0的结果,不知道是不是我理解有误。谢谢
from cpp_concurrency_in_action.
Related Issues (20)
- 第二章有句话感觉没有体现原文意思 HOT 2
- 清单3.4 线程安全的堆栈类定义错误 HOT 1
- 少了一个字符 HOT 1
- 3.2.3节问题 HOT 2
- 4.2.2 翻译的根本看不懂··· HOT 1
- 中文字体 HOT 1
- 第三章 3.2 有错字 HOT 1
- 3.2.3 节 选项2 HOT 1
- Problem with lock-free queue in chapter 7. HOT 7
- Pages can not be opened HOT 1
- 请教第五章里面的一个问题 HOT 8
- 原版 HOT 1
- 第二版出来了,还有兴趣翻译么? HOT 3
- 5.2.7 Free functions for atomic operations HOT 1
- 5.2.3 std::atomic<bool> 提供的exchange(), 不仅仅是一个“读-改-写”的操作 HOT 1
- 6.1 这被称为序列化(serialzation): 线程轮流访问被保护的数据 HOT 1
- 翻译的感觉还不如看英文呢老哥。。 HOT 1
- Now the font in the book becomes bigger, how to change it to be small ? HOT 1
- 第五章 <C++内存模型与原子操作> 有句话读不通 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpp_concurrency_in_action.