Results 1 to 4 of 4

Thread: QThreads, QMutexes, and multiple access

  1. #1
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QThreads, QMutexes, and multiple access

    QMutex is used to lock access to a variable so multiple access from separate threads do not overwrite/read data at the same time it is possibly changing.

    If I have an object that is shared between threads and they are only reading variables (that are constant and will never change) do I still need to lock them with a QMutex?

    Second part of this is on code that is changing values can I pass around an object that self locks similar to something like:

    Qt Code:
    1. class myDataObj : public QObject {
    2.  
    3. Q_OBJECT
    4.  
    5. public:
    6. QMtuex var1locker;
    7. QMutex var2locker;
    8.  
    9. qint32 readvar1();
    10. void setvar1(int newvar1);
    11. qint32 readvar2;
    12. void setvar2(int newvar2);
    13.  
    14. private:
    15. qint32 var1;
    16. qint32 var2;
    17.  
    18. };
    19.  
    20.  
    21. qint32 myDataObj::readvar1() {
    22.  
    23. qint32 return1;
    24. var1locker.lock()
    25. return1 = var1;
    26. var1locker.unlock();
    27.  
    28. return return1;
    29. }
    30.  
    31. void myDataObj::setvar1(int newvar1) {
    32.  
    33. var1locker.lock();
    34. var1 = newvar1;
    35. var1locker.unlock();
    36. }
    37.  
    38. ...
    To copy to clipboard, switch view to plain text mode 
    Is this a viable idea to wrap the QMutex in the object itself so I can just pass the object around to all the threads and have the object lock itself rather then having mutexes that have to be shared across all threads to control access?

    Bob

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThreads, QMutexes, and multiple access

    It might be better to use QReadWriteLock instead of QMutex here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QThreads, QMutexes, and multiple access

    Than you for the response Wysota but I think there might be a mis interpretation of my questions.

    QReadWriteLock are just simplified convince class for a QMutex and not needed in this situation. My Questions to be more to the point are if a variable is essentially a constant does it really need a lock when being read. Logic says no but I wanted to see if anyone had any wisdom in that area.

    And the examples on QThread are very basic (which is fine I just needed more information) and was not sure if I should try to maintain global for QMutexes when it seemed more logical to just keep them inside the object and let the object control access to the data which felt more OOP.
    The example uses a global QMutex since is just really a snippet and needed a way for the 2 classes to share a lock.

    bob

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThreads, QMutexes, and multiple access

    In most cases read-only variables don't need to be protected. That's exactly what QReadWriteLock does - it lets many readers in but when a writer wants to step in, all readers and other writers become blocked.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Tangled in QThreads
    By spawn9997 in forum Qt Programming
    Replies: 7
    Last Post: 14th October 2009, 22:00
  2. Using QTimer in QThreads
    By tzhu in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2009, 11:12
  3. Identifying QThreads
    By TheGrimace in forum Newbie
    Replies: 2
    Last Post: 18th March 2008, 16:39
  4. Qthreads
    By Sheetal in forum Qt Programming
    Replies: 5
    Last Post: 23rd March 2007, 11:12
  5. QTcpSockets and QThreads
    By TheRonin in forum Newbie
    Replies: 3
    Last Post: 21st June 2006, 09:41

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.