PDA

View Full Version : When use QReadWriteLock and when use QMutex?



Momergil
29th April 2014, 15:27
Hello!

I just discovered the existence of this class, QReadWriteLock, and when reading the Qt Assistant files, I noticed it seems very much like QMutex. In fact, the documentation states "In many cases, QReadWriteLock is a direct competitor to QMutex." The problem is that no comparison table was provided to learn when it's appropriate to use which of them, nor teeling in which situations there is no difference between using one or the other.

My question is, therefore: when should one use QReadWriteLock instead of QMutex, and when the contrary should be done?

Thanks,

Momergil

anda_skoa
29th April 2014, 18:10
A read-write lock allows multiple readers to hold the lock simultaniously, while a writer will always have exclusive "ownership".

Its implementation is more complex than a regular mutex (since it needs to distinguish between two types of locking), roughly the complexity of two normal mutexes.

Main use case is when you have lots of independent readers and only few, probably one, writer.

Let say when you have one thread generating image data and several thread reading that image data and encoding it in various file formats, sizes, etc.
The readers can all simultaniously access the image data without interfering with each other.

Cheers,
_

Momergil
6th May 2014, 18:56
Thanks anda_skoa for the reply!

Momergil