fork(1) download
  1. #include <iostream>
  2. #include <mutex>
  3.  
  4. using namespace std;
  5.  
  6. #define DO_ONCE(callable) do { static bool _do_once_ = (callable(), true); (void)_do_once_; }while (false)
  7.  
  8. #define \
  9.  DO_ONCE2(callable) do { \
  10.  static std::once_flag _do_once_ ;\
  11.  std::call_once(_do_once_, callable); \
  12.  \
  13.   } while (false)
  14.  
  15. void once_log()
  16. {
  17. cout << "once call\n";
  18. }
  19.  
  20. void call(int a)
  21. {
  22. DO_ONCE([a] {
  23. cout << "once: " << a << endl;
  24. });
  25.  
  26. DO_ONCE(once_log);
  27.  
  28. DO_ONCE2([a] {
  29. cout << "once2: " << a << endl;
  30. });
  31.  
  32. cout << "call: " << a << endl;
  33. }
  34.  
  35. int main()
  36. {
  37. call(1);
  38. call(2);
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
once: 1
once call
once2: 1
call: 1
call: 2