memcheck/tests/sh-mem-random.c: Set huge_addr to 240GB
[valgrind.git] / drd / tests / condvar.cpp
blob18ecb3f8a483955516139d625e6f6da159c9c96f
1 /* See also https://bugs.kde.org/show_bug.cgi?id=445504 */
3 #include <condition_variable>
4 #include <future>
5 #include <iostream>
6 #include <mutex>
7 #include <thread>
8 #include <vector>
10 using lock_guard = std::lock_guard<std::mutex>;
11 using unique_lock = std::unique_lock<std::mutex>;
13 struct state {
14 std::mutex m;
15 std::vector<int> v;
16 std::condition_variable cv;
18 state() {
19 // Call pthread_cond_init() explicitly to let DRD know about 'cv'.
20 pthread_cond_init(cv.native_handle(), NULL);
24 void other_thread(state *sp) {
25 state &s = *sp;
26 std::cerr << "Other thread: waiting for notify\n";
27 unique_lock l{s.m};
28 while (true) {
29 if (s.cv.wait_for(l, std::chrono::seconds(3)) !=
30 std::cv_status::timeout) {
31 std::cerr << "Other thread: notified\n";
32 break;
35 return;
39 int main() {
40 state s;
41 auto future = std::async(std::launch::async, other_thread, &s);
43 if (future.wait_for(std::chrono::seconds(1)) != std::future_status::timeout) {
44 std::cerr << "Main: other thread returned too early!\n";
45 return 2;
49 std::lock_guard<std::mutex> g{s.m};
50 s.v.push_back(1);
51 s.v.push_back(2);
52 s.cv.notify_all();
54 return 0;