Home | History | Annotate | Download | only in thread.once.callonce
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // UNSUPPORTED: libcpp-has-no-threads
     11 
     12 // <mutex>
     13 
     14 // struct once_flag;
     15 
     16 // template<class Callable, class ...Args>
     17 //   void call_once(once_flag& flag, Callable&& func, Args&&... args);
     18 
     19 // This test is supposed to be run with ThreadSanitizer and verifies that
     20 // call_once properly synchronizes user state, a data race that was fixed
     21 // in r280621.
     22 
     23 #include <mutex>
     24 #include <thread>
     25 #include <cassert>
     26 
     27 std::once_flag flg0;
     28 long global = 0;
     29 
     30 void init0()
     31 {
     32     ++global;
     33 }
     34 
     35 void f0()
     36 {
     37     std::call_once(flg0, init0);
     38     assert(global == 1);
     39 }
     40 
     41 int main()
     42 {
     43     std::thread t0(f0);
     44     std::thread t1(f0);
     45     t0.join();
     46     t1.join();
     47     assert(global == 1);
     48 }
     49