xref: /llvm-project/libcxx/test/std/thread/thread.semaphore/binary.pass.cpp (revision fa1c077b41ae1335332d65399802f2c68e82ca7b)
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 // UNSUPPORTED: c++03, c++11
11 
12 // This test requires the dylib support introduced in D68480, which shipped in macOS 11.0.
13 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14|15}}
14 
15 // <semaphore>
16 
17 #include <semaphore>
18 #include <chrono>
19 #include <thread>
20 #include <type_traits>
21 
22 #include "make_test_thread.h"
23 #include "test_macros.h"
24 
25 static_assert(std::is_same<std::binary_semaphore, std::counting_semaphore<1>>::value, "");
26 
27 int main(int, char**)
28 {
29   std::binary_semaphore s(1);
30 
31   auto l = [&](){
32     for(int i = 0; i < 1024; ++i) {
33         s.acquire();
34         std::this_thread::sleep_for(std::chrono::microseconds(1));
35         s.release();
36     }
37   };
38 
39   std::thread t = support::make_test_thread(l);
40   l();
41 
42   t.join();
43 
44   return 0;
45 }
46