Does std::condition_variable::wait_until have any advantage against std::this_thread::sleep_for?

Zhang

In a time waiting scenario:

our software works in the background, and synchronizes data with the server in every 20 - 30 minutes.

I wanted to use

std::this_thread::sleep_for

But my superior strongly against any form of sleep function. He recommends

std::condition_variable::wait_until(lock, timeout-time, pred)

I wonder if there are any disadvantage for sleep_for under such scenario?

Timo

As pointed out in the comments already, it depends only on your usecase. The main difference between the two is, that condition_variable can wake up earlier if you trigger it. You also can add a predicate that must be satisfied in order to actually wake up, but that's only a quality of life addition. And btw, the equivalent to sleep_for is wait_for and not wait_until. condition_variable is also great to communicate or synchronize between multiple threads.

Given everything you said, I would use condition_variable for the following reasons:

  1. Putting a thread to sleep for longer periods of time is not a good idea because your application can exit at any time (or rather can be requested to exit). In that case you probably want your thread to exit properly, so you have to be able to wake it up at any time.
  2. You want to change the config on the fly. If your thread has to restart with new parameters or if you need that thread to actually load the config file you also don't want to wait for the next 20min intervall to end.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

std::condition_variable not working with std::this_thread::sleep_for()

Can std::this_thread::sleep_for() have spurious wakeups?

std::this_thread::sleep_for() freezing the program

implementation of std::condition_variable::wait_until

std::this_thread::sleep_for() vs sleep() in main()

Code generated - usleep vs std::this_thread::sleep_for

What is s in std::this_thread::sleep_for(2s)?

std::this_thread::sleep_for sleeps for too long

Unexpected behavior with `std::chrono::system_clock` and `std::chrono::steady_clock` in `std::condition_variable::wait_until`

Why is `std::this_thread::yield()` 10x slower than `std::this_thread::sleep_for(0s)`?

How to make a thread stop excution (eg: std::this_thread::sleep_for) for an accturate interval

Why does this code sporadically hang on std::condition_variable::wait()?

How does std::condition_variable::wait() evaluate the given predicate?

std::this_thread::sleep_for sleeps shorter than expected in VS2015

Why doesn't std::this_thread::sleep_for() work in MacOS terminal?

std::condition_variable – notify once but wait thread wakened twice

Are std::condition_variable wait predicates thread-safe?

What is the maximum value I can pass to std::thread::sleep_for() and sleep_until()?

Why does std::condition_variable as a class member cause compile errors with std::thread?

Is std::condition_variable thread-safe?

How is std::condition_variable::wait implemented?

std::condition_variable::wait in c++

Do I have a risk to put a thread in deadlock with std::condition_variable in C++?

std::condition_variable why does it need a std::mutex

C++11 std::this_thread - How to cancel sleep_until ()?

C++11 std::this_thread::sleep_until() hangs when compiled with GCC 4.8.5

std::this_thread::sleep_until timing is completely off by about a factor of 2, inexplicably

std::condition_variable::notify_one() does not wake up a waiting thread

Why does std::condition_variable::wait_for() return with timeout if timeout too big