Код: Выделить всё
std::condition_variable cv;
std::mutex m_cnt;
int cnt = 0;
void producer() {
std::unique_lock ul(m_cnt);
cnt++;
cv.notify_one();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
void consumer() {
std::unique_lock ul(m_cnt);
cv.wait(ul, [] {return cnt > 0;}); // ---> Here
cnt--;
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... e-the-lock
Мобильная версия