FreeBSD regtest: add missing helgrind expecteds
[valgrind.git] / helgrind / tests / bug392331.cpp
blobff26883b76ef72004304a64457992e12278791f9
1 // For this Bugzilla item https://bugs.kde.org/show_bug.cgi?id=392331
2 // Example from https://en.cppreference.com/w/cpp/thread/condition_variable
4 #include <iostream>
5 #include <string>
6 #include <thread>
7 #include <mutex>
8 #include <condition_variable>
10 std::mutex m;
11 std::condition_variable cv;
12 std::string data;
13 bool ready = false;
14 bool processed = false;
16 void worker_thread()
18 // Wait until main() sends data
19 std::unique_lock lk(m);
20 cv.wait(lk, []{return ready;});
22 // after the wait, we own the lock.
23 std::cout << "Worker thread is processing data\n";
24 data += " after processing";
26 // Send data back to main()
27 processed = true;
28 std::cout << "Worker thread signals data processing completed\n";
30 // Manual unlocking is done before notifying, to avoid waking up
31 // the waiting thread only to block again (see notify_one for details)
32 lk.unlock();
33 cv.notify_one();
36 int main()
38 std::thread worker(worker_thread);
40 data = "Example data";
41 // send data to the worker thread
43 std::lock_guard lk(m);
44 ready = true;
45 std::cout << "main() signals data ready for processing\n";
47 cv.notify_one();
49 // wait for the worker
51 std::unique_lock lk(m);
52 cv.wait(lk, []{return processed;});
54 std::cout << "Back in main(), data = " << data << '\n';
56 worker.join();