dimanche 15 août 2021

std::condition_variable::wait_for() wakes up the thread later than requested

I'm using std::condition_variable::wait_for(), and the maximum amount in ms to wait for is 30ms, but i noticed that the wait time is much longer than that. This is how I use the wait_for:

std::chrono::milliseconds timeToWait(30);
std::unique_lock<std::mutex> lck(mMutex);
std::chrono::high_resolution_clock::duration timeToWaitDuration(timeToWait);
auto t1 = std::chrono::high_resolution_clock::now();
std::cv_status cvStatus = mCondVar.wait_for(lck, timeToWaitDuration);
auto t2 = std::chrono::high_resolution_clock::now();
LOGI(0, "Waited for %d ms", std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1));

Possible output:

Waited for 102 ms

Waited for 159 ms

Waited for 181 ms

How is it possible? Shouldn't the thread be awaken when the time ends? Thanks.

Aucun commentaire:

Enregistrer un commentaire