[Boost-users] [chrono][thread] Best way to wait?

109 views
Skip to first unread message

Klaim - Joël Lamotte

unread,
Mar 29, 2013, 11:23:34 AM3/29/13
to Boost users list
Hi,
can someone clarify:
1. what is the difference between:

    const auto wait_time = boost::chrono::miliseconds( 8 );
    boost::mutex mutex;
    boost::unique_lock<boost::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

Steven Watanabe

unread,
Mar 29, 2013, 11:37:43 AM3/29/13
to boost...@lists.boost.org
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_lock<boost::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

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Klaim - Joël Lamotte

unread,
Mar 29, 2013, 11:45:39 AM3/29/13
to Boost users list

On Fri, Mar 29, 2013 at 4:37 PM, Steven Watanabe <watan...@gmail.com> 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?
So basically a condition_variable can wait less, while sleep_for can wait a bit more?

Joel Lamotte

Steven Watanabe

unread,
Mar 29, 2013, 11:58:32 AM3/29/13
to boost...@lists.boost.org
AMDG

On 03/29/2013 08:45 AM, Klaim - Joël Lamotte wrote:
> On Fri, Mar 29, 2013 at 4:37 PM, Steven Watanabe <watan...@gmail.com>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?
>

Klaim - Joël Lamotte

unread,
Mar 29, 2013, 12:13:48 PM3/29/13
to Boost users list

On Fri, Mar 29, 2013 at 4:58 PM, Steven Watanabe <watan...@gmail.com> wrote:
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.

I see, thanks for the clarification.

Joel Lamotte

Reply all
Reply to author
Forward
0 new messages