Code Monkey home page Code Monkey logo

Comments (4)

xjj210130 avatar xjj210130 commented on May 2, 2024

同时,即使是6,7两个线程交叉执行, 线程6中,x的赋值也是优于于y的赋值,那么x.load()也是true。
会执行z++的.
不知道我这个理解哪里有问题,谢谢

from cpp_concurrency_in_action.

xiaoweiChen avatar xiaoweiChen commented on May 2, 2024

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.

xjj210130 avatar xjj210130 commented on May 2, 2024

from cpp_concurrency_in_action.

xjj210130 avatar xjj210130 commented on May 2, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.