Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 444
Zhang Avatar asked Jul 02 '19 09:07

Zhang


People also ask

What is the use of wait_until in STD STD?

std::condition_variable wait_until causes the current thread to block until the condition variable is notified, a specific time is reached, or a spurious wakeup occurs, optionally looping until some predicate is satisfied. 1) Atomically releases lock, blocks the current executing thread, and adds it to the list of threads waiting on *this.

How does a thread wait for a condition variable?

When using a condition variable to wait for a condition, a thread performs the following sequence of steps: It determines that the condition is not currently true. It starts waiting for some other thread to make the condition true. This is the wait call.

What does condition_variable wait do in C++?

std::condition_variable wait causes the current thread to block until the condition variable is notified or a spurious wakeup occurs, optionally looping until some predicate is satisfied. 1) Atomically unlocks lock, blocks the current executing thread, and adds it to the list of threads waiting on *this.

How does wait work in C++11?

(2) (since C++11) wait causes the current thread to block until the condition variable is notified or a spurious wakeup occurs, optionally looping until some predicate is satisfied. 1) Atomically unlocks lock, blocks the current executing thread, and adds it to the list of threads waiting on *this.


Video Answer


1 Answers

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.
like image 100
Timo Avatar answered Oct 19 '22 17:10

Timo