All challenges
Code AnalysisHard 60s

Condition variable lost wakeup with a missing lock

What is the race in this code?
Starter code
#include <condition_variable>
#include <mutex>
#include <queue>

std::queue<int> q;
std::mutex m;
std::condition_variable cv;

void produce() {
    q.push(1);
    cv.notify_one();   // no lock held
}
void consume() {
    if (q.empty()) cv.wait(std::unique_lock<std::mutex>(m));
    q.pop();
}

Think you can answer under pressure?

Get matched and answer live questions in 3-minute 1v1 battles. Elo ranking, badge tiers and bragging rights on the line.

Play this in a 3-minute battle