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
8 #include <condition_variable>
11 std::condition_variable cv
;
14 bool processed
= false;
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()
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)
38 std::thread
worker(worker_thread
);
40 data
= "Example data";
41 // send data to the worker thread
43 std::lock_guard
lk(m
);
45 std::cout
<< "main() signals data ready for processing\n";
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';