Damian
Damian

Reputation: 4631

wake up thread at specified time

I want to wake up my threads a specified time. Can I do it like this?

#include <chrono>
#include <thread>
using namespace std::literals::chrono_literals;
void foo() {}
int main()
{
    using clock = std::chrono::steady_clock;
    clock::time_point next_time_point = clock::now() + 20s;
    foo();
    std::this_thread::sleep_until(next_time_point);
}

Upvotes: 1

Views: 51

Answers (1)

crn
crn

Reputation: 31

You never should use sleep. Instead wait on conditional variable

Upvotes: 3

Related Questions