1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-threads
11 // This test occasionally fails on Android.
12 // UNSUPPORTED: LIBCXX-ANDROID-FIXME
14 // <condition_variable>
16 // class condition_variable;
21 // NOTE: `notify_one` is just a wrapper around pthread_cond_signal, but
22 // POSIX does not guarantee that one and only one thread will be woken:
24 // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_signal.html
27 // Multiple Awakenings by Condition Signal
28 // On a multi-processor, it may be impossible for an implementation of
29 // pthread_cond_signal() to avoid the unblocking of more than one thread
30 // blocked on a condition variable. For example...
34 // NOTE: In previous versions of this test, `notify_one` was called WITHOUT
35 // holding the lock but POSIX says (in the aforementioned URL) that:
36 // ...if predictable scheduling behavior is required, then that mutex shall
37 // be locked by the thread calling pthread_cond_broadcast() or
38 // pthread_cond_signal().
41 #include <condition_variable>
47 #include "make_test_thread.h"
48 #include "test_macros.h"
51 std::condition_variable cv
;
54 std::atomic_int
test1(0);
55 std::atomic_int
test2(0);
56 std::atomic_int
ready(2);
57 std::atomic_int
which(0);
61 std::unique_lock
<std::mutex
> lk(mut
);
73 std::unique_lock
<std::mutex
> lk(mut
);
85 std::thread t1
= support::make_test_thread(f1
);
86 std::thread t2
= support::make_test_thread(f2
);
89 std::this_thread::yield();
91 // 1) Both f1 and f2 have entered their condition variable wait.
92 // 2) Either f1 or f2 has the mutex locked and is about to wait.
93 std::unique_lock
<std::mutex
> lk(mut
);
101 std::this_thread::yield();
102 std::unique_lock
<std::mutex
> lk(mut
);
118 std::this_thread::yield();
119 std::unique_lock
<std::mutex
> lk(mut
);