Add PR check to suggest alternatives to using undef (#118506)
[llvm-project.git] / libcxx / test / std / thread / thread.jthread / join.deadlock.pass.cpp
blob21fbacde36cb9004ce842b905aee3e9d9fc6a78f
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 // Windows cannot detect the deadlock. Instead of throwing system_error,
10 // it would dead lock the test
11 // UNSUPPORTED: windows
13 // TSAN bug: https://github.com/llvm/llvm-project/issues/66537
14 // UNSUPPORTED: tsan
16 // UNSUPPORTED: no-threads
17 // UNSUPPORTED: no-exceptions
18 // UNSUPPORTED: c++03, c++11, c++14, c++17
19 // XFAIL: availability-synchronization_library-missing
21 // void join();
23 #include <atomic>
24 #include <cassert>
25 #include <chrono>
26 #include <concepts>
27 #include <functional>
28 #include <system_error>
29 #include <thread>
30 #include <type_traits>
31 #include <vector>
33 #include "make_test_thread.h"
34 #include "test_macros.h"
36 int main(int, char**) {
37 // resource_deadlock_would_occur - if deadlock is detected or get_id() == this_thread::get_id().
39 std::function<void()> f;
40 std::atomic_bool start = false;
41 std::atomic_bool done = false;
43 std::jthread jt = support::make_test_jthread([&] {
44 start.wait(false);
45 f();
46 done = true;
47 done.notify_all();
48 });
50 f = [&] {
51 try {
52 jt.join();
53 assert(false);
54 } catch (const std::system_error& err) {
55 assert(err.code() == std::errc::resource_deadlock_would_occur);
58 start = true;
59 start.notify_all();
60 done.wait(false);
63 return 0;