1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
// atomic::operator=/operator T example:
#include <iostream> // std::cout
#include <atomic> // std::atomic
#include <thread> // std::thread, std::this_thread::yield
std::atomic<int> foo = 0;
void set_foo(int x) {
foo = x;
}
void print_foo() {
while (foo==0) { // wait while foo=0
std::this_thread::yield();
}
std::cout << "foo: " << foo << '\n';
}
int main ()
{
std::thread first (print_foo);
std::thread second (set_foo,10);
first.join();
second.join();
return 0;
}
| |