Re: waiting for another thread without blocking resources...
In article <61iuq7F1v9ofdU1@mid.dfncis.de>,
Lars Uffmann <aral@nurfuerspam.de> wrote:
Cholo Lennon wrote:
If you are using boost, just call 'join':
Thank you, now I got the problem that join() never returns, instead my
application crashes - AFTER the last line of code in the thread of
execution, and before the line following the join statement in the
calling event procedure... Someone doesn't like me here.
What's worse, the news portal to the boost mailing list causes my
thunderbird to create a severe error message upon attempting to post...
Does anyone happen to have a link to a good boost documentatin?
http://boost.org/libs/libraries.htm doesn't quite cut it...
Well, boost is kind of on-topic here so:
IMO, boost should be seen as a collection of libraries, not as one
monolithic lib. Some of these libraries have better documentation
than others. For boost::thread, IMO,
http://www.boost.org/doc/html/thread.html is perfectly adequate
documentation.
You are probably interested in:
boost::condition
boost::condition::wait
Download the boost archive and go through the tutorial in
../boost/libs/thread/tutorial/
and the examples in:
../boost/libs/thread/example/
The bounded_buffer.cpp and monitor.cpp examples should be of interest
to you. In your type of application you probably want independent GUI
thread and service thread that communicate exclusively using a shared
EventQueue that is mutex protected. In the pop/receive/read method of
your EventQueue, you lock the mutex, check if there is some Event on
the queue, if yes, de-queue and return that event, if not call a
condition.wait. The push/send/write method is responsible to signal
the condition if it adds an event to a previously empty queue.
class EventQueue
{
private:
boost::mutex m_mutex;
boost::condition m_condition;
// an actual containers e.g. std::queue
}
Event EventQeueue::receive()
{
boost::ScopedLock L(m_mutex);
if( /* the container is empty (e.g. m_queue.empty() )
{
m_condition.wait(L);
}
Event ev = /* get an Event from the container */
return ev;
}
in the send(Event const &) method, uses condition::notify_all() or
condition::notify_one() to signal possibly waiting threads if the
container has become non-empty (e.g. size == 1). You may also want to
use while(/*container empty*/) rather than if()
The above can be written generic using templates as a container
adaptor (or inheritance). E.g.
class TreadSafeContainer< std::queue< Event > > EventQueue;
I think boost discussion are OK here. If you want to talk more
spacifically about multithreading issues themselves
comp.programming.threads might be a forum of interest to you.
Yan