
hi, i am doing a job and considering if boost.thread can help. what i am doing is writing some code for other one's use. my code will be called by many threads, and accessing some shared data. my problem is how to keep my code thread-safe. i have no way to control how those other threads were created ( actually, those had already done by another team using windows MFC, and, for some reasons, my code can not use MFC, is has to be portable bwteen Linux and Windows ). in breif, you can think my code in a way of producer/consumer pattern. the producer is a rouine which will be called by many threads ( which are actaully connected sockets created by a listening socket ), and the consumer routine will be called by another thread ( which is actually GUI's main thread ). what i expected is: 1, only one producer can write to the shared data; 2, when a producer is writting, other producers have to busy waiting; and the GUI thread should immediately exit; 3, when GUI thread is reading, no producer can write to the shared data; to my problem, does the boost.thread can help? thanks in advance. -- woody then sun rose thinly from the sea and the old man could see the other boats, low on the water and well in toward the shore, spread out across the current.

Steven Woody wrote:
hi,
i am doing a job and considering if boost.thread can help.
...
in breif, you can think my code in a way of producer/consumer pattern. the producer is a rouine which will be called by many threads ( which are actaully connected sockets created by a listening socket ), and the consumer routine will be called by another thread ( which is actually GUI's main thread ). what i expected is:
1, only one producer can write to the shared data; 2, when a producer is writting, other producers have to busy waiting; and the GUI thread should immediately exit; 3, when GUI thread is reading, no producer can write to the shared data;
From the sound of what you're trying to do then yes. This page shows how to make a queue and add and remove items from it. http://www.boostcookbook.com/Recipe:/1234841 K

On 10/2/07, Kirit Sælensminde
Steven Woody wrote:
hi,
i am doing a job and considering if boost.thread can help.
...
in breif, you can think my code in a way of producer/consumer pattern. the producer is a rouine which will be called by many threads ( which are actaully connected sockets created by a listening socket ), and the consumer routine will be called by another thread ( which is actually GUI's main thread ). what i expected is:
1, only one producer can write to the shared data; 2, when a producer is writting, other producers have to busy waiting; and the GUI thread should immediately exit; 3, when GUI thread is reading, no producer can write to the shared data;
From the sound of what you're trying to do then yes. This page shows how to make a queue and add and remove items from it. http://www.boostcookbook.com/Recipe:/1234841
K
god! i found the boost's documentation is very hard to read, it is not for a beginner! and, it seems if i want to use the mutex and lock, the threads themself have to be created using boost, is it true? it's impossible to me since the threads had been created by other member of our team and i am just writing library codes for them. any other inputs will be highly appreciated! - woody

Woody, All you have to do is protect the shared data with a boost::mutex. You will want to protect any methods that access or mutate the data. Boost has a nice "scoped_lock". The scoped_lock will set a lock and automatically unset the lock when it goes out of scope. If multiple threads reach this code, the first thread will set the lock and the other threads will wait until the lock is unset. This effectively makes the section of code protected by a scoped_lock single threaded. Here is a quick-and-dirty example: class ThreadSafeData { public: typedef std::vector<float> FloatVector; // Accessor (return by value) FloatVector getFloats (void) const { boost::mutex::scoped_lock lock(m_Mutex); return m_Floats; } // Mutator void addFloat (float value) { boost::mutex::scoped_lock lock(m_Mutex); m_Floats.push_back(value); } // Mutator void clearFloats (void) { boost::mutex::scoped_lock lock(m_Mutex); m_Floats.clear(); } private: boost::mutex m_Mutex; FloatVector m_Floats; } Notice I am returning data by value instead of const reference! You do not want multiple threads keeping references around to shared data. Also, notice that this code does not concern itself with how the threads are created. It simply takes care of protecting the data from multiple threads (however they are created). I hope that this helps! Justin On Wednesday 03 October 2007 09:44:34 Steven Woody wrote:
On 10/2/07, Kirit Sælensminde
wrote: Steven Woody wrote:
hi,
i am doing a job and considering if boost.thread can help.
...
in breif, you can think my code in a way of producer/consumer pattern. the producer is a rouine which will be called by many threads ( which are actaully connected sockets created by a listening socket ), and the consumer routine will be called by another thread ( which is actually GUI's main thread ). what i expected is:
1, only one producer can write to the shared data; 2, when a producer is writting, other producers have to busy waiting; and the GUI thread should immediately exit; 3, when GUI thread is reading, no producer can write to the shared data;
From the sound of what you're trying to do then yes. This page shows how to make a queue and add and remove items from it. http://www.boostcookbook.com/Recipe:/1234841
K
god! i found the boost's documentation is very hard to read, it is not for a beginner! and, it seems if i want to use the mutex and lock, the threads themself have to be created using boost, is it true? it's impossible to me since the threads had been created by other member of our team and i am just writing library codes for them.
any other inputs will be highly appreciated!
- woody

On 10/4/07, KSpam
Woody,
All you have to do is protect the shared data with a boost::mutex. You will want to protect any methods that access or mutate the data. Boost has a nice "scoped_lock". The scoped_lock will set a lock and automatically unset the lock when it goes out of scope. If multiple threads reach this code, the first thread will set the lock and the other threads will wait until the lock is unset. This effectively makes the section of code protected by a scoped_lock single threaded. Here is a quick-and-dirty example:
class ThreadSafeData { public: typedef std::vector<float> FloatVector;
// Accessor (return by value) FloatVector getFloats (void) const { boost::mutex::scoped_lock lock(m_Mutex); return m_Floats; }
// Mutator void addFloat (float value) { boost::mutex::scoped_lock lock(m_Mutex); m_Floats.push_back(value); }
// Mutator void clearFloats (void) { boost::mutex::scoped_lock lock(m_Mutex); m_Floats.clear(); }
private: boost::mutex m_Mutex; FloatVector m_Floats; }
Notice I am returning data by value instead of const reference! You do not want multiple threads keeping references around to shared data. Also, notice that this code does not concern itself with how the threads are created. It simply takes care of protecting the data from multiple threads (however they are created).
I hope that this helps!
Justin
thanks a lot for your code which cleared most of my question. while the scoped_lock can has a thread block when a mutex object is being locked by another thread, can you show me additional example about how to let a thread immediately exit when it found a mutex object is being used? i mean that one of thread in our application is actually the GUI main thread, it will access the shared data in it's OnIdel event handler, so i hope it will be never blocked, when resources are not available, i hope it can immediately exist and try again some times later. does the boost support this kind of feature? thanks. - woody

On 10/4/07, Steven Woody
On 10/4/07, KSpam
wrote: Woody,
All you have to do is protect the shared data with a boost::mutex. You will want to protect any methods that access or mutate the data. Boost has a nice "scoped_lock". The scoped_lock will set a lock and automatically unset the lock when it goes out of scope. If multiple threads reach this code, the first thread will set the lock and the other threads will wait until the lock is unset. This effectively makes the section of code protected by a scoped_lock single threaded. Here is a quick-and-dirty example:
class ThreadSafeData { public: typedef std::vector<float> FloatVector;
// Accessor (return by value) FloatVector getFloats (void) const { boost::mutex::scoped_lock lock(m_Mutex); return m_Floats; }
// Mutator void addFloat (float value) { boost::mutex::scoped_lock lock(m_Mutex); m_Floats.push_back(value); }
// Mutator void clearFloats (void) { boost::mutex::scoped_lock lock(m_Mutex); m_Floats.clear(); }
private: boost::mutex m_Mutex; FloatVector m_Floats; }
Notice I am returning data by value instead of const reference! You do not want multiple threads keeping references around to shared data. Also, notice that this code does not concern itself with how the threads are created. It simply takes care of protecting the data from multiple threads (however they are created).
I hope that this helps!
Justin
sorry for i repost below message for i really need your kind help. ----------------------- thanks a lot for your code which cleared most of my question. while the scoped_lock can has a thread block when a mutex object is being locked by another thread, can you show me additional example about how to let a thread immediately exit when it found a mutex object is being used? i mean that one of thread in our application is actually the GUI main thread, it will access the shared data in it's OnIdel event handler, so i hope it will be never blocked, when resources are not available, i hope it can immediately exist and try again some times later. does the boost support this kind of feature? thanks. -------------------------

Woody, On Thursday 04 October 2007 08:05:44 Steven Woody wrote:
thanks a lot for your code which cleared most of my question. while the scoped_lock can has a thread block when a mutex object is being locked by another thread, can you show me additional example about how to let a thread immediately exit when it found a mutex object is being used? i mean that one of thread in our application is actually the GUI main thread, it will access the shared data in it's OnIdel event handler, so i hope it will be never blocked, when resources are not available, i hope it can immediately exist and try again some times later. does the boost support this kind of feature? thanks.
It sounds like you might want to use a try_mutex and a scoped_try_lock. I have not personally used this construct, but you can find documentation in the following locations: http://boost.org/doc/html/boost/try_mutex.html http://boost.org/doc/html/thread/concepts.html#thread.concepts.ScopedTryLock http://boost.org/doc/html/thread/concepts.html#thread.concepts.TryLock The only example I see in the boost distribution (1.34.1) is libs/thread/test/test_mutex.cpp. Justin

Steven Woody wrote:
On 10/2/07, Kirit Sælensminde
wrote: Steven Woody wrote:
hi,
i am doing a job and considering if boost.thread can help.
...
in breif, you can think my code in a way of producer/consumer pattern. the producer is a rouine which will be called by many threads ( which are actaully connected sockets created by a listening socket ), and the consumer routine will be called by another thread ( which is actually GUI's main thread ). what i expected is:
1, only one producer can write to the shared data; 2, when a producer is writting, other producers have to busy waiting; and the GUI thread should immediately exit; 3, when GUI thread is reading, no producer can write to the shared data; From the sound of what you're trying to do then yes. This page shows how to make a queue and add and remove items from it. http://www.boostcookbook.com/Recipe:/1234841
K
god! i found the boost's documentation is very hard to read, it is not for a beginner! and, it seems if i want to use the mutex and lock, the threads themself have to be created using boost, is it true? it's impossible to me since the threads had been created by other member of our team and i am just writing library codes for them.
The thread can be created in any way and the mutexes, locks and conditions will work. The Boost documentation is fine so long as you already understand threading issues. Shared state threading is very, very hard and something you should expect to get wrong more often than not even after a lot of practice. The trick is to share as little as possible. If you can get away with sharing nothing between threads then it is actually very simple to do. K
participants (3)
-
Kirit Sælensminde
-
KSpam
-
Steven Woody