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?
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:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments