Is there a C ++ equivalent for java.util.concurrent.locks.ReentrantReadWriteLock?

I would like to control access to getters and setters for a bunch of data classes to make them safely accessible from multiple threads at the same time. I already did this in Java using java.util.concurrent.locks.ReentrantReadWriteLock, and it was pretty painless.

But now I have a lot of problems in my current C ++ project, because I cannot find a reimplementation of read / write lock. In particular, I want a stream to allow a stream to receive a read lock if it already has a write lock, without a lock, and without giving up write lock.

The reason is simple: some of my setter methods call getter methods, and the former (usually) get write locks, and the latter read locks. I don't want to distort my straightforward getter / setter architecture in order to get around the restrictions in lock classes.

I tried Qt (4.8) QReadWriteLock and its related classes and Boost unique_lock and shared_lock. None of the libraries implements the update I need. Does any other part of Boost use this?

Or is there another library that has this? I'm really surprised that neither Qt nor Boost look like they seem to be such a clearly desirable feature. (And which has been part of the Java standard library since 2004.)

+8
java c ++ multithreading boost qt
source share
1 answer

Like someone who has done multitasking with C ++, I'm not sure if you find this feature in a widespread library. As far as I know, increasing streams, std streams and POSIX streams does not allow this.

As a result, to solve your problem, I would suggest the following:

  • Look again at the lock pattern and the data you share. Is there any other way that you can accomplish what you are going to do?

  • Try to implement your own version of the lock initiators you create. It should be just a simple extension of the primitives that are already available in any library you choose. Also, share it with the community, as I'm sure you will not be the only one with this problem.

+3
source share

All Articles