views:

710

answers:

6

Did any body implemented a Monitor with signaling (wake up waiting threads) using a mutex and condition variables in C++.

I dont know how to start. Any sample code or online article will be great. Are there any open source libraries who have implemented these?

I need for windows and linux. But to start with windows(win32) will be fine.

A: 

All threading programming I've done in C++ used pthreads, which has the fundamental building blocks you need. This article discusses using those building blocks for Monitors

For some reason the link doesn't work, paste the same URL into a browser and it does.

http://findarticles.com/p/articles/mi_m0VVT/is_10_2/ai_n24998136/
djna
The link is not working.
San
Weird ... the URL does work.
djna
A: 

I made one here for posix systems: http://asgaard.homelinux.org/svn/cpp/threadqueue/ There's a C variant here: http://asgaard.homelinux.org/wordpress/?p=9

nos
A: 

Mozilla's platform abstraction library NSPR (the Netscape Portable Runtime) has a monitor abstraction which is cross-platform. It's a "real" implementation (actually used in Firefox for example) so it's not simple. But it is a monitor.

Source: prmon.c, prmon.h

Documentation: prmon.html

quark
+2  A: 

This Qt Quarterly article explains how to do this using Qt's QMutex and QWaitCondition. But you should be able to reimplement it with whatever mutex class you want to use..

See also the more advanced example in here..

Kristian
+1 very clear article, and very easy to apply to other toolkits
iain
A: 

See my answer to this post it may help you.

Ben
Sorry I didn't notice that you asked for linux *and* windows. This will work only with posix systems
Ben
+1  A: 

Check out boost::thread::condition_variable together with samples. It can be used to wait for the condition with or without a timeout. I think it's a fairly elegant solution which should do exactly what you need in this case, and do it in a portable way.

da_m_n