Results 1 to 4 of 4

Thread: Why is QMutex Thread Safe?

  1. #1
    Join Date
    Feb 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Why is QMutex Thread Safe?

    Re: http://qt.nokia.com/doc/4.0/threads.html

    At the above URL we are given the following example:

    Qt Code:
    1. class Counter
    2. {
    3. public:
    4. Counter() { n = 0; }
    5.  
    6. void increment() { QMutexLocker locker(&mutex); ++n; }
    7. void decrement() { QMutexLocker locker(&mutex); --n; }
    8. int value() const { QMutexLocker locker(&mutex); return n; }
    9.  
    10. private:
    11. mutable QMutex mutex;
    12. int n;
    13. };
    To copy to clipboard, switch view to plain text mode 


    What makes this thread safe? I assume the QMutexLocker constructor is not atomic (I could be wrong). If two threads call increment, a QMutexLocker would be created. This will in part check to see that mutex is not currently locked. Is it not possible for both threads to call increment and both simultaneously begin to create a QMutexLocker, both QMutexLocker constructor calls simultaneously check the lock state of mutex, both simultaneously confirm that mutex is not currently locked, both lock mutex, both increment n, and then release. Ending up with the same non-thread safe issue you'd have if a mutex was not used at all.

    Is a QMutexLocker more about lowering the chance of threading issues and not actually eliminating them? Perhaps I've overlooked something.
    Last edited by Kind Lad; 20th February 2010 at 08:24.

  2. #2
    Join Date
    Feb 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why are QMutex Thread Safe?

    This post should be deleted.
    Last edited by Kind Lad; 20th February 2010 at 09:28.

  3. #3
    Join Date
    Feb 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why is QMutex Thread Safe?

    Does nobody know the answer?

  4. #4
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why is QMutex Thread Safe?

    Quote Originally Posted by Kind Lad View Post
    What makes this thread safe?
    Qt is open source; the details are in the source code.

    A quick look shows that QMutex is implemented using platform-specific operating system calls and QAtomicInt, which in turn is implemented using platform/compiler-specific code (for example, some inline assembly code is used under Windows/MinGW). At that point it’s beyond my understanding, but that’s where you’ll have to look if you want to know, in detail, why it’s thread-safe.

    QMutexLocker is a convenience for managing a QMutex using the RAII paradigm. The QMutexLocker constructor is not atomic, but it will block at the point where it tries to lock the associated QMutex if another thread has already locked the QMutex, because the call to QMutex::lock within the QMutexLocker constructor will block and wait until the lock has been released.

Similar Threads

  1. Thread Safe Queue container....
    By jcox23 in forum Newbie
    Replies: 25
    Last Post: 5th June 2012, 19:36
  2. About the QSound,is thread safe?
    By cspp in forum Qt Programming
    Replies: 0
    Last Post: 6th November 2009, 14:26
  3. Is a QProcess thread safe in Qt4?
    By Jay_D in forum Qt Programming
    Replies: 4
    Last Post: 1st September 2009, 16:38
  4. What makes something not thread safe?
    By tgreaves in forum Newbie
    Replies: 9
    Last Post: 20th February 2009, 20:16
  5. QT emit/signaling thread-safe?
    By richy in forum Qt Programming
    Replies: 1
    Last Post: 14th July 2006, 09:37

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.