Results 1 to 3 of 3

Thread: When use QReadWriteLock and when use QMutex?

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default When use QReadWriteLock and when use QMutex?

    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
    Last edited by Momergil; 29th April 2014 at 14:28. Reason: spelling corrections
    May the Lord be with you. Always.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: When use QReadWriteLock and when use QMutex?

    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,
    _

  3. The following 2 users say thank you to anda_skoa for this useful post:

    Lumbricus (20th January 2016), Momergil (6th May 2014)

  4. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: When use QReadWriteLock and when use QMutex?

    Thanks anda_skoa for the reply!

    Momergil
    May the Lord be with you. Always.

Similar Threads

  1. QReadWriteLock locks for reading...but why?
    By Qtonimo in forum Qt Programming
    Replies: 5
    Last Post: 29th August 2012, 11:58
  2. Overriding QReadWriteLock's lockForRead and lockForWrite
    By mentalmushroom in forum Qt Programming
    Replies: 6
    Last Post: 19th July 2011, 14:47
  3. QMutex QMutex::Recursive is not a type name
    By Qiieha in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2011, 14:01
  4. QReadWriteLock Question
    By qtYoda in forum Newbie
    Replies: 9
    Last Post: 7th April 2011, 11:03
  5. using QReadWriteLock in QMap and Qhash
    By HERC in forum Qt Programming
    Replies: 0
    Last Post: 1st July 2010, 13:00

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.