microsoft / microsoft/STL

`<condition_variable>`: The actual waiting time of `cv.wait_for()` is longer than duration param

Open
#2,173 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Describe the bug
It is found that the time interval is longer than the duration setting while using conditional_varaiable.wait_for(). If the duration is set 50ms, the actual waiting time is about 60ms..

Test case

#include <stdio.h>
#include <stdlib.h>
#include <thread>
#include <chrono>
#include <string.h>
#include <condition_variable>
#include <memory>
#include <fstream>
#include <list>
#include <vector>
#include <mutex>
std::mutex mtx;
std::condition_variable point_pack_condition;
using namespace std::chrono;

void wait()
{
    int i = 0;
    steady_clock::time_point last_time = steady_clock::now();
    for (i = 0; i < 20; ++i) {
        {
            std::unique_lock<std::mutex> lock(mtx);
            last_time = steady_clock::now();
            printf("%d\n",point_pack_condition.wait_for(lock, milliseconds(50)));
            printf("Delta time:%ld.\n", duration_cast<microseconds>(steady_clock::now() - last_time).count());
            last_time = steady_clock::now();
        }

        printf("Finish save %d frame to lvx file.\n", i);
    }
}

int main(int argc, const char *argv[]) {
  std::thread t1(wait);
  t1.join();
  getchar();
}

Expected behavior
The code tested on Ubuntu16.04 or win7 behaves as follows:
image

Actual behavior
On win10:
image

Version
Windows10
visual studio 2019
vc_tools:14.29.30133

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied C++ reproducer and compare std::condition_variable::wait_for timing on Windows 10 against the reported Ubuntu and Windows 7 results. Trace the Windows implementation and its clock or scheduler behavior to determine whether the longer wait violates the timeout contract. Done means establishing the cause and adding an appropriate regression test or documenting why the observed timing is permitted.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.