Boost logo

Boost Users :

Subject: Re: [Boost-users] Threading question
From: Davies, John (john.davies_at_[hidden])
Date: 2013-09-27 17:19:30

The farther I dig into Boost Interprocess the unhappier I become. Every wait seems to come down to a tight loop that is polling a condition.

I'm looking into condition_variables and Asio to see if it's more of the same.


John Davies
Home Respiratory Care
Philips Home Healthcare Solutions
1740 Golden Mile Highway
Monroeville, PA 15146

Email: john.davies_at_[hidden]
Fax: 724-387-4109

-----Original Message-----
From: Boost-users [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Gavin Lambert
Sent: Thursday, September 26, 2013 7:49 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] Threading question

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 don't want a sleep if
> possible.

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 mailing list

The information contained in this message may be confidential and legally protected under applicable law. The message is intended solely for the addressee(s). If you are not the intended recipient, you are hereby notified that any use, forwarding, dissemination, or reproduction of this message is strictly prohibited and may be unlawful. If you are not the intended recipient, please contact the sender by return e-mail and destroy all copies of the original message.

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at