Results 1 to 6 of 6

Thread: QReadWriteLock locks for reading...but why?

  1. #1
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QReadWriteLock locks for reading...but why?

    Hi,
    I have a big ressource and several Threads want to read the ressource simultanous. The ressource get exchanged every hour. While exchanging the read-access should be blocked. I solve this with QReadWriteLocker.
    But why the Threads are locked for reading. If one thread reads a lot of data, all the other threads are locked, because of QReadLocker.

    I just want to lock the threads, while exchanging...

    Thanks, bye
    Last edited by Qtonimo; 28th August 2012 at 09:38.
    Pursue your targets and don't give up....

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QReadWriteLock locks for reading...but why?

    If you QReadWriteLock::lockForWrite() the file to write it blocks users in other threads (as documented) until you unlock().

    What exactly does "exchanged" mean?

  3. #3
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QReadWriteLock locks for reading...but why?

    Thanks for reply

    Exchanged means renewed...

    While renew() the read-access should be blocked....
    Qt Code:
    1. void DataManager::renew(){
    2. QWriteLocker(&lock);
    3. QList<Data*>* data = getnewdata();
    4.  
    5. //clear old data
    6. delete this->data;
    7.  
    8. //assign new data
    9. this->data = data;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Simultaneous read should be possible, but QReadLocker prevent this.
    Pursue your targets and don't give up....

  4. #4
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QReadWriteLock locks for reading...but why?

    Nobody can help?
    I just want to know why several Threads aren't able to read the same container at the same time?
    Pursue your targets and don't give up....

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QReadWriteLock locks for reading...but why?

    Active QReadLockers don't block other read locks but an open write lock will.

    Here's an example showing three concurrent read locks and no blocks:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. class SlowReader: public QObject {
    5. Q_OBJECT
    6. public:
    7. SlowReader(int number, QReadWriteLock *lock, QObject *p = 0)
    8. : QObject(p), m_num(number), m_lock(lock)
    9. {
    10. }
    11.  
    12. public slots:
    13. void process() {
    14. QReadLocker locker(m_lock);
    15. qDebug() << QString("Thread %1 started").arg(m_num);
    16. for (int i = 0; i < 10; ++i) {
    17. sleep(1);
    18. qDebug() << QString("Thread %1 reading").arg(m_num);
    19. }
    20. qDebug() << QString("Thread %1 done").arg(m_num);
    21. emit finished();
    22. }
    23.  
    24. signals:
    25. void finished();
    26.  
    27. private:
    28. int m_num;
    29. QReadWriteLock *m_lock;
    30. };
    31.  
    32.  
    33. int main(int argc, char **argv) {
    34. QCoreApplication app(argc, argv);
    35.  
    36. for (int i= 0; i < 3; ++i) {
    37. QThread* thread = new QThread;
    38. SlowReader *worker = new SlowReader(i, &lock);
    39. worker->moveToThread(thread);
    40.  
    41. QObject::connect(thread, SIGNAL(started()), worker, SLOT(process()));
    42. QObject::connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
    43. QObject::connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
    44. QObject::connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    45. thread->start();
    46. }
    47.  
    48. return app.exec();
    49. }
    50.  
    51. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Each thread holds a read lock for the entire 10 seconds it takes to do the 10 reads.

  6. The following user says thank you to ChrisW67 for this useful post:

    Qtonimo (29th August 2012)

  7. #6
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QReadWriteLock locks for reading...but why?

    Thank you ChrisW67 for the test app.
    It convinced me.
    Pursue your targets and don't give up....

Similar Threads

  1. QwtPlotZoomer locks up, possibly bug?
    By Neekeetos in forum Qwt
    Replies: 12
    Last Post: 10th February 2012, 12:49
  2. Overriding QReadWriteLock's lockForRead and lockForWrite
    By mentalmushroom in forum Qt Programming
    Replies: 6
    Last Post: 19th July 2011, 14:47
  3. QReadWriteLock Question
    By qtYoda in forum Newbie
    Replies: 9
    Last Post: 7th April 2011, 11:03
  4. using QReadWriteLock in QMap and Qhash
    By HERC in forum Qt Programming
    Replies: 0
    Last Post: 1st July 2010, 13:00
  5. QThread locks my gui ??
    By LordQt in forum Qt Programming
    Replies: 8
    Last Post: 8th December 2008, 19:53

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.