fork download
  1. #include <iostream>
  2.  
  3. static volatile int *entered;
  4.  
  5. void once(void) {
  6. int result;
  7. result = __sync_val_compare_and_swap(entered, 0, 1); // cmpxchg になる
  8.  
  9. if (result == 1) {
  10. return;
  11. }
  12. std::cout << "once" << std::endl;
  13. }
  14.  
  15. int main() {
  16. entered = new int;
  17. *entered = 0;
  18.  
  19. once();
  20. once();
  21. return 0;
  22.  
  23. delete entered;
  24. }
  25. /* end */
  26.  
Success #stdin #stdout 0s 4500KB
stdin
Standard input is empty
stdout
once