[chrono][thread] Best way to wait?
Hi, can someone clarify: 1. what is the difference between: const auto wait_time = boost::chrono::miliseconds( 8 ); boost::mutex mutex; boost::unique_lockboost::mutex lock( mutex ); boost::condition_variable wait_condition; wait_condition.wait_for( lock, wait_time ); and const auto wait_time = boost::chrono::miliseconds( 8 ); boost::this_thread::sleep_for( wait_time ); 2. are there other ways to wait? (I mean make the thread sleep, not looping until it's time) Joel Lamotte
AMDG On 03/29/2013 08:23 AM, Klaim - Joël Lamotte wrote:
Hi, can someone clarify: 1. what is the difference between:
const auto wait_time = boost::chrono::miliseconds( 8 ); boost::mutex mutex; boost::unique_lockboost::mutex lock( mutex ); boost::condition_variable wait_condition; wait_condition.wait_for( lock, wait_time );
and
const auto wait_time = boost::chrono::miliseconds( 8 ); boost::this_thread::sleep_for( wait_time );
A thread waiting on a condition variable (a) can be explicitly notified and (b) may wake up before the time has expired (spurious wakeups). If you just want to sleep for a fixed amount of time, then sleep_for is the right tool.
2. are there other ways to wait? (I mean make the thread sleep, not looping until it's time)
In Christ, Steven Watanabe
On Fri, Mar 29, 2013 at 4:37 PM, Steven Watanabe
A thread waiting on a condition variable (a) can be explicitly notified and (b) may wake up before the time has expired (spurious wakeups). If you just want to sleep for a fixed amount of time, then sleep_for is the right tool.
My understanding is that sleep_for can wait more than the time wanted, right? So basically a condition_variable can wait less, while sleep_for can wait a bit more? Joel Lamotte
AMDG On 03/29/2013 08:45 AM, Klaim - Joël Lamotte wrote:
On Fri, Mar 29, 2013 at 4:37 PM, Steven Watanabe
wrote: A thread waiting on a condition variable (a) can be explicitly notified and (b) may wake up before the time has expired (spurious wakeups). If you just want to sleep for a fixed amount of time, then sleep_for is the right tool.
My understanding is that sleep_for can wait more than the time wanted, right?
You can always wait longer than you ask for. A thread can wait even if you don't ask to wait. It's entirely dependent on the kernel's scheduling algorithm.
So basically a condition_variable can wait less, while sleep_for can wait a bit more?
In Christ, Steven Watanabe
participants (2)
-
Klaim - Joël Lamotte
-
Steven Watanabe