Results 1 to 5 of 5

Thread: Does a QReadWriteLock need to be the same in order to be valid?

  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 Does a QReadWriteLock need to be the same in order to be valid?

    Hello!

    I have implemented an application where, apart from the main thread, there are two other threads that access the same QVector: one only writes into it while the other reads and erase (QVector::clear()). In order to prevent problems, I created a QReadWriteLock on each of the two classes and I call them when needed (either for "lockForWrite()" in the first thread or "lockForRead()" in the second).

    The problem is that, while debugging, I found out that after calling the lockForRead() and inside its scope the QVector was actually being edited by the first thread (which had itself called lockForWrite() before doing that). In other words, the locking wasn't working \o/

    So does this means that, in order for the locking mechanism to work, I need to always consider the same locker? In other words, I must either declare one of the lockers as public and access it from the other thread or else use "extern" to access it? And is this valid also for mutexes?


    Thanks,

    Momergil
    May the Lord be with you. Always.

  2. #2
    Join Date
    May 2014
    Location
    Minas Gerais, Brazil
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Does a QReadWriteLock need to be the same in order to be valid?

    Hi!

    You really need to use only one Mutex/Locker, using two mutexes/lockers would mean that you want to lock the access to other objects or following another logic.

    As you may know, lock for reading allows more than one thread to read it at a time. Lock for writing will allow only one thread to write. But this applies only if you're using the same locker.

  3. The following user says thank you to Rafaelfsilva for this useful post:

    Momergil (29th May 2014)

  4. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Does a QReadWriteLock need to be the same in order to be valid?

    (...) two other threads that access the same QVector: one only writes into it while the other reads and erase (QVector::clear()).
    "lockForWrite()" in the first thread or "lockForRead()" in the second
    This looks like a bug to me. Both threads are modifying the same data structure ("clear" is also a kind of "write", as it changes the object), and without proper synchronization you have a race condition here - simply use QMutex in both threads to solve this. Or use read/write lock but "clear" only in the first thread.

  5. The following user says thank you to stampede for this useful post:

    Momergil (29th May 2014)

  6. #4
    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: Does a QReadWriteLock need to be the same in order to be valid?

    Quote Originally Posted by Momergil View Post
    I created a QReadWriteLock on each of the two classes
    Just to sure sure: you only have one instance of the lock that you share between the instances of the classes involved in accessing the vector?

    Cheers,
    _

  7. #5
    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: Does a QReadWriteLock need to be the same in order to be valid?

    Quote Originally Posted by anda_skoa View Post
    Just to sure sure: you only have one instance of the lock that you share between the instances of the classes involved in accessing the vector?

    Cheers,
    _
    Actually not: the situation, as correctly interpreted by the other two commentators, was that I had two lockers, one defined on each thread's private section in the header file. No wonder why I experienced some crashes

    But situation was normalized and now I noticed the locking mechanism is working correctly.


    Thanks,

    Momergil
    May the Lord be with you. Always.

Similar Threads

  1. When use QReadWriteLock and when use QMutex?
    By Momergil in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2014, 17:56
  2. QReadWriteLock locks for reading...but why?
    By Qtonimo in forum Qt Programming
    Replies: 5
    Last Post: 29th August 2012, 11:58
  3. Overriding QReadWriteLock's lockForRead and lockForWrite
    By mentalmushroom in forum Qt Programming
    Replies: 6
    Last Post: 19th July 2011, 14:47
  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.