Boost Users :
Subject: Re: [Boost-users] Threading question
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2013-09-26 19:48:57
On 9/27/2013 11:16 AM, Quoth Davies, John:
> I have the following code and it terminates as expected. When I call the
> destructor it interrupts the thread.
> But if I change the this_thread::sleep to this_thread::yield the join()
> never happens and the program hangs.
> I must be misunderstanding yield. But I really dont want a sleep if
The main thing to remember about yield() is that it's permitted to be a
no-op if the OS feels like it, and in particular on single-core systems
it will completely block lower priority threads from running.
Also note that unlike sleep, yield is not listed as an interruption
point. So when you're using yield you're basically setting up a 100%
CPU loop that can't be interrupted, which is why it's hanging. You
*could* fix part of that by adding an explicit interruption point, but
that's not the ideal solution.
Given that it looks like you basically want to block until there's work
to do in the queue, you might want to consider using a mutex and
condition_variable (notifying the condition variable when you push
something into the queue, and waiting on it when the queue is empty).
You're going to need a mutex on the queue operations anyway unless
you're using something that's internally thread-safe.
You also might want to consider using something like Boost.Asio instead.
Its io_service allows you to queue arbitrary function objects to a
specific thread (or threadpool) in a thread-safe manner, even if you
don't want to use the actual I/O parts.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net