views:

613

answers:

4

How to write a Reader/Writer lock with timeout, using conditional variables in C/C++?

+1  A: 

You may take a look at the source of Java's implementation ReentrantReadWriteLock.

Just grab the JDK unpack the src.zip file and search for the source.

Boris Pavlović
Sorry, I forgot to mention: The solution should be in C/C++
Sabya
It's ok. At least you can borrow some ideas from Java's implementation
Boris Pavlović
A: 

There is no support for such things in current standard C++, although C++0x will have some threading support (I haven't checked to see how much). Therefore, any answer would have to be platform-dependent, using platform in a very general sense.

It is possible to write libraries that will behave similarly across different underlying systems, and there are quite a few cross-platform libraries. (These are subject to inefficiencies, of course, if the capabilities of the underlying platforms are different.) There could be a generally accepted C++ threading library that would work on Windows and Unix-based systems (which is almost anything you'd be running on a desktop nowadays), but I don't know of one offhand.

Therefore, this question can't really be answered as asked. It would be necessary to answer it on a particular platform or library, and none is specified. It would make sense if coupled with a request for a library recommendation.

David Thornley
+3  A: 

If you're just looking for a library, Boost.Thread might do what you want.

sth
A: 

Take a look at the Boost.Thread library. Specifically: shared_mutex and upgradeable_mutex objects.

Ferruccio