Skip to content

Commit 4f3fff1

Browse files
author
lulongfei
committed
update atomic
1 parent 45f1db4 commit 4f3fff1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

atomic.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
#include <atomic>
3+
#include <thread>
4+
5+
6+
std::atomic<int> foo = 0;
7+
8+
void set_foo(int x)
9+
{
10+
foo = x;
11+
}
12+
13+
void print_foo()
14+
{
15+
while(foo == 0)
16+
{
17+
std::this_thread::yield();
18+
}
19+
std::cout << "foo: " << foo << std::endl;
20+
}
21+
22+
23+
int main()
24+
{
25+
std::thread first(print_foo);
26+
std::thread second(set_foo, 10);
27+
first.join();
28+
second.join();
29+
return 0;
30+
}
31+

0 commit comments

Comments
 (0)