1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // UNSUPPORTED: libcpp-has-no-threads 10 11 // <mutex> 12 13 // struct once_flag; 14 15 // template<class Callable, class ...Args> 16 // void call_once(once_flag& flag, Callable&& func, Args&&... args); 17 18 // This test is supposed to be run with ThreadSanitizer and verifies that 19 // call_once properly synchronizes user state, a data race that was fixed 20 // in r280621. 21 22 #include <mutex> 23 #include <thread> 24 #include <cassert> 25 26 std::once_flag flg0; 27 long global = 0; 28 29 void init0() 30 { 31 ++global; 32 } 33 34 void f0() 35 { 36 std::call_once(flg0, init0); 37 assert(global == 1); 38 } 39 40 int main(int, char**) 41 { 42 std::thread t0(f0); 43 std::thread t1(f0); 44 t0.join(); 45 t1.join(); 46 assert(global == 1); 47 48 return 0; 49 } 50