Следующий код C++ с сайта cppreference.com иллюстрирует, как std::condition_variable используется в сочетании с std::mutex для облегчить взаимодействие между потоками.
Код: Выделить всё
#include //01
#include //02
#include //03
#include //04
#include //05
//06
std::mutex m; //07
std::condition_variable cv; //08
std::string data; //09
bool ready = false; //10
bool processed = false; //11
//12
void worker_thread() { //13
// wait until main() sends data //14
std::unique_lock lk(m); //15
cv.wait(lk, [] { return ready; }); //16
//17
// after the wait, we own the lock //18
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78190875/why-does-valgrind-based-helgrind-thread-error-detector-reports-data-races-that-v[/url]