[Thread] What do I #include?
Hi all, This may seem like a silly question, but is someone able to point me to where in the Boost docs it tells me which files I need to #include to use Boost Threads? I'm looking at the docs here: http://www.boost.org/doc/libs/1_35_0/doc/html/thread.html But the only #include directive I can find is for using boost::barrier. There's no mention of what to use for boost::mutex for instance, and even the example code that comes in the distribution doesn't say what to include to use boost::shared_mutex. So far I've only managed to find what I needed by recursively grepping the installed .hpp files, so I'm sure I'm missing something! Thanks, Adam.
Adam Nielsen
This may seem like a silly question, but is someone able to point me to where in the Boost docs it tells me which files I need to #include to use Boost Threads?
It doesn't. Oops. I'll raise a trac issue for it.
You can include
This may seem like a silly question, but is someone able to point me to where in the Boost docs it tells me which files I need to #include to use Boost Threads?
It doesn't. Oops. I'll raise a trac issue for it.
Ah, glad it's not me being dumb for once :-)
You can include
to get everything.
Does this include shared_mutex? I was still getting errors about
boost::shared_mutex when I included
Adam Nielsen
This may seem like a silly question, but is someone able to point me to where in the Boost docs it tells me which files I need to #include to use Boost Threads?
It doesn't. Oops. I'll raise a trac issue for it.
Ah, glad it's not me being dumb for once :-)
You can include
to get everything. Does this include shared_mutex? I was still getting errors about boost::shared_mutex when I included
, which prompted my hunt through the docs in the first place!
It does in the trunk version, but not in 1.35.0
shared_mutex is in
The way I use mutex is to protect one shared resource. Here is sample
code that I cooked up. I am not sure why you want to use a single mutex
to protect multiple resources. However, if your use case demands it than
wrap those resources in a class and have a mutex as a member variable of
that class.
HTH.
Call this file let say ThreadSafeQueue.hpp
#include
This may seem like a silly question, but is someone able to point me
to where in the Boost docs it tells me which files I need to #include to use Boost Threads?
It doesn't. Oops. I'll raise a trac issue for it.
Ah, glad it's not me being dumb for once :-)
You can include
to get everything. Does this include shared_mutex? I was still getting errors about boost::shared_mutex when I included
, which prompted
my hunt through the docs in the first place!
It does in the trunk version, but not in 1.35.0
shared_mutex is in
"Khandelwal, Amit"
The way I use mutex is to protect one shared resource. Here is sample code that I cooked up. I am not sure why you want to use a single mutex to protect multiple resources. However, if your use case demands it than wrap those resources in a class and have a mutex as a member variable of that class.
template
class tsqueue { const T& front() { boost::lock_guardboost::mutex lock(mutex); std::cout << "Pop element: " << m_queue.front() << std::endl; return m_queue.front(); }
void pop() { boost::lock_guardboost::mutex lock(mutex); return m_queue.pop(); }
This code is not thread safe unless there is only one consumer. Though each operation is safe, the front()/pop() model gives you a race condition: some other thread may steal your element between the call to front() and pop(). See http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-... for a description of a thread-safe queue that uses a condition variable to provide waiting and allows multiple consumers. Anthony -- Anthony Williams | Just Software Solutions Ltd Custom Software Development | http://www.justsoftwaresolutions.co.uk Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL
Hmm...I don't know why you say that there will be a race condition. Let
say 2 threads are calling pop on the queue and another thread is calling
front(). Let us call these 3 thread p1, p2 (for pop) and f1 for front.
As per my understanding - this could be one possible scenario.
P1 -- acquire the lock
P2 -- blocked on the lock
F1 -- blocked on the lock
P1 -- done and waiting on the lock
P2 -- still blocking
F1 -- acquired the lock
So where is the race condition ?
-----Original Message-----
From: boost-users-bounces@lists.boost.org
[mailto:boost-users-bounces@lists.boost.org] On Behalf Of Anthony
Williams
Sent: Friday, July 11, 2008 9:14 AM
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] [Thread] What do I #include?
"Khandelwal, Amit"
The way I use mutex is to protect one shared resource. Here is sample code that I cooked up. I am not sure why you want to use a single mutex to protect multiple resources. However, if your use case demands
it than wrap those resources in a class and have a mutex as a member variable of that class.
template
class tsqueue { const T& front() { boost::lock_guardboost::mutex lock(mutex); std::cout << "Pop element: " << m_queue.front() << std::endl; return m_queue.front(); }
void pop() { boost::lock_guardboost::mutex lock(mutex); return m_queue.pop(); }
This code is not thread safe unless there is only one consumer. Though each operation is safe, the front()/pop() model gives you a race condition: some other thread may steal your element between the call to front() and pop(). See http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-s afe-queue-using-condition-variables.html for a description of a thread-safe queue that uses a condition variable to provide waiting and allows multiple consumers. Anthony -- Anthony Williams | Just Software Solutions Ltd Custom Software Development | http://www.justsoftwaresolutions.co.uk Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice. -------- IRS Circular 230 Disclosure: Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.
"Khandelwal, Amit"
Hmm...I don't know why you say that there will be a race condition. Let say 2 threads are calling pop on the queue and another thread is calling front(). Let us call these 3 thread p1, p2 (for pop) and f1 for front.
As per my understanding - this could be one possible scenario.
P1 -- acquire the lock P2 -- blocked on the lock F1 -- blocked on the lock
P1 -- done and waiting on the lock P2 -- still blocking F1 -- acquired the lock
So where is the race condition ?
Two threads retrieving a value: each does some_var=queue.front(); queue.pop(); Though each operation is thread-safe, the combination isn't, because they can interleave badly: T1: some_var=queue.front(); T2: some_var=queue.front(); // oops we just got the same value T1: queue.pop(); T2: queue.pop(); // Oops the second value in the queue just got //discarded without being read This is a race condition: the outcome depends on the ordering of operations in separate threads. There's a similar problem with empty(). Suppose each thread does if(!queue.empty()) { some_var=queue.front(); queue.pop(); } Two threads can interleave again to give a race condition: T1: if(!queue.empty()) // queue is not empty T2: if(!queue.empty()) // queue is not empty T1: some_var=queue.front(); T1: queue.pop(); T2: some_var=queue.front(); // oops reading non-existent element T2: queue.pop(); // oops no element to pop. Designing interfaces for multi-threaded use is not as simple as just slapping a mutex on the data and locking it in every member function. Anthony -- Anthony Williams | Just Software Solutions Ltd Custom Software Development | http://www.justsoftwaresolutions.co.uk Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL
I see what you mean to say. I overlooked the fact that the user will
call front and pop one after another. Thanks.
-----Original Message-----
From: boost-users-bounces@lists.boost.org
[mailto:boost-users-bounces@lists.boost.org] On Behalf Of Anthony
Williams
Sent: Friday, July 11, 2008 10:56 AM
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] [Thread] What do I #include?
"Khandelwal, Amit"
Hmm...I don't know why you say that there will be a race condition. Let say 2 threads are calling pop on the queue and another thread is calling front(). Let us call these 3 thread p1, p2 (for pop) and f1 for front.
As per my understanding - this could be one possible scenario.
P1 -- acquire the lock P2 -- blocked on the lock F1 -- blocked on the lock
P1 -- done and waiting on the lock P2 -- still blocking F1 -- acquired the lock
So where is the race condition ?
Two threads retrieving a value: each does some_var=queue.front(); queue.pop(); Though each operation is thread-safe, the combination isn't, because they can interleave badly: T1: some_var=queue.front(); T2: some_var=queue.front(); // oops we just got the same value T1: queue.pop(); T2: queue.pop(); // Oops the second value in the queue just got //discarded without being read This is a race condition: the outcome depends on the ordering of operations in separate threads. There's a similar problem with empty(). Suppose each thread does if(!queue.empty()) { some_var=queue.front(); queue.pop(); } Two threads can interleave again to give a race condition: T1: if(!queue.empty()) // queue is not empty T2: if(!queue.empty()) // queue is not empty T1: some_var=queue.front(); T1: queue.pop(); T2: some_var=queue.front(); // oops reading non-existent element T2: queue.pop(); // oops no element to pop. Designing interfaces for multi-threaded use is not as simple as just slapping a mutex on the data and locking it in every member function. Anthony -- Anthony Williams | Just Software Solutions Ltd Custom Software Development | http://www.justsoftwaresolutions.co.uk Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice. -------- IRS Circular 230 Disclosure: Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.
Khandelwal, Amit wrote:
The way I use mutex is to protect one shared resource. Here is sample code that I cooked up. I am not sure why you want to use a single mutex to protect multiple resources. However, if your use case demands it than wrap those resources in a class and have a mutex as a member variable of that class.
HTH.
Call this file let say ThreadSafeQueue.hpp
#include
#include <iostream> #include <queue> #include <deque> namespace std { namespace threadsafe {
template
class tsqueue { private: std::queue
m_queue; boost::mutex mutex; public:
[snip]
const T& front() { boost::lock_guardboost::mutex lock(mutex); std::cout << "Pop element: " << m_queue.front() << std::endl; return m_queue.front(); }
Ouch. Returning an element by reference could easily lead to trouble when the container is accessed from multiple threads. / Johan
Agreed. I didn't realize that I had typed return by reference. My bad. -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Johan Nilsson Sent: Friday, July 11, 2008 9:42 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [Thread] What do I #include? Khandelwal, Amit wrote:
The way I use mutex is to protect one shared resource. Here is sample code that I cooked up. I am not sure why you want to use a single mutex to protect multiple resources. However, if your use case demands
it than wrap those resources in a class and have a mutex as a member variable of that class.
HTH.
Call this file let say ThreadSafeQueue.hpp
#include
#include <iostream> #include <queue> #include <deque> namespace std { namespace threadsafe {
template
class tsqueue { private: std::queue
m_queue; boost::mutex mutex; public:
[snip]
const T& front() { boost::lock_guardboost::mutex lock(mutex); std::cout << "Pop element: " << m_queue.front() << std::endl; return m_queue.front(); }
Ouch. Returning an element by reference could easily lead to trouble when the container is accessed from multiple threads. / Johan _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice. -------- IRS Circular 230 Disclosure: Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.
Even if the element were returned by value, the following code would
suffer from the race condition:
element = my_conatiner.front();
// here -- the front element might already have been popped here by
another thread!
my_container.pop();
or:
if (!my_container.empty())
{
// the container might already be empty here!
element = my_container.front();
}
2008/7/11, Khandelwal, Amit
Agreed. I didn't realize that I had typed return by reference. My bad.
participants (5)
-
Adam Nielsen
-
Anthony Williams
-
Igor R
-
Johan Nilsson
-
Khandelwal, Amit