sno*_*ain 1 c++ multithreading
我已将其初始化为
count = 15;
Run Code Online (Sandbox Code Playgroud)
如果我有两个这样的线程:
thread_1(){
count = 0;
x = count;
count = x;
}
Run Code Online (Sandbox Code Playgroud)
和
thread_2(){
y = count;
count = y;
count = 0;
}
Run Code Online (Sandbox Code Playgroud)
在没有计数同步的情况下,在运行两个线程之后,count可以最终为15.
如果我将我的count变量声明为原子,那么在运行两个线程后,count会一直是0吗?
没有; 程序可以像执行一样执行
count = 15 (全局初始化)y = count (线程2)count = 0 (线程1)count = y (thread2 - > 15)x = count (线程1)count = 0 (线程2)count = x (thread1 - > 15)