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