Skip to content

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

@meng-zha

Description

@meng-zha

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationRelated to documentation or comments

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions