Results 1 to 8 of 8

Thread: Why QMutex.lock() doesn't return any code?

  1. #1
    Join Date
    Sep 2009
    Location
    Poland, Cracow
    Posts
    34
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Why QMutex.lock() doesn't return any code?

    I wonder why QMutex.lock() (and unlock()) does not return any result code?
    pthreads provide return code, wxWidgets provide error codes in their wxMutex (http://docs.wxwidgets.org/trunk/classwx_mutex.html). Why does Qt not do the same? What will happen in Qt app if underlying pthread_* function would fail? What chances are for the application to recover from it?

  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: Why QMutex.lock() doesn't return any code?

    One of the reasons might be that the mutex API for Windows does not provide any return code. Besides any of the return codes for wxWidgets mutexes do not make sense with Qt's mutexes.
    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
    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: Why QMutex.lock() doesn't return any code?

    Just guessing: those error conditions might not be available on all platforms.

    Some of pthread's error conditions also can't happen. For example EINVAL cannot happen because QMutex has always initialized the platform mutex before calling any operations on it.

    It seems the only missing error is EAGAIN, when a recursive mutex cannot be further locked because its recursion depth is reached.
    But then again needing a recursive mutex is almost always a sign for too complex critical sections, reaching the max recursion depth would be a dead give-away.

    Cheers,
    _

  4. #4
    Join Date
    Sep 2009
    Location
    Poland, Cracow
    Posts
    34
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Why QMutex.lock() doesn't return any code?

    What about implementations of QMutexPrivate constructor? Code for win:
    Qt Code:
    1. QMutexPrivate::QMutexPrivate()
    2. {
    3. event = CreateEvent(0, FALSE, FALSE, 0);
    4. if (!event)
    5. qWarning("QMutexData::QMutexData: Cannot create event");
    6. }
    To copy to clipboard, switch view to plain text mode 

    Code for unix:
    Qt Code:
    1. static void report_error(int code, const char *where, const char *what)
    2. {
    3. if (code != 0)
    4. qWarning("%s: %s failure: %s", where, what, qPrintable(qt_error_string(code)));
    5. }
    6.  
    7. QMutexPrivate::QMutexPrivate()
    8. : wakeup(false)
    9. {
    10. report_error(pthread_mutex_init(&mutex, NULL), "QMutex", "mutex init");
    11. qt_initialize_pthread_cond(&cond, "QMutex");
    12. }
    To copy to clipboard, switch view to plain text mode 

    Constructor initializes synchronization primitive, then if it failed, just reports it with warning and keeps going:
    Qt Code:
    1. bool QMutexPrivate::wait(int timeout)
    2. {
    3. report_error(pthread_mutex_lock(&mutex), "QMutex::lock", "mutex lock");
    4. ...
    To copy to clipboard, switch view to plain text mode 

    Similar code is for Windows. Is that right to just warn about the problem and then execute another functions on it?! Shouldn't the application be aware that there was a problem with the mutex, so it can stop doing what it's doing, maybe retry, or allow any other kind of recovery strategy?

  5. #5
    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: Why QMutex.lock() doesn't return any code?

    Read again why a mutex might fail and how many of such siuations apply to QMutex. Furthermore as far as I remember it uses a real mutex only if it detects that another thread is already in the critical section.
    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.


  6. #6
    Join Date
    Sep 2009
    Location
    Poland, Cracow
    Posts
    34
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Why QMutex.lock() doesn't return any code?

    Well:
    The pthread_mutex_init() function will fail if:
    [EAGAIN]
    The system lacked the necessary resources (other than memory) to initialise another mutex.
    [ENOMEM]
    Insufficient memory exists to initialise the mutex.
    [EPERM]
    The caller does not have the privilege to perform the operation.

    [EBUSY]
    The implementation has detected an attempt to re-initialise the object referenced by mutex, a previously initialised, but not yet destroyed, mutex.
    [EINVAL]
    The value specified by attr is invalid.
    The EAGAIN seems very much important and capable for some recovery. EPERM looks legit too. I can only guess what are other possible problems like that (not only for lock() method and not only for pthread). What about that?

  7. #7
    Join Date
    Sep 2009
    Location
    Poland, Cracow
    Posts
    34
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Why QMutex.lock() doesn't return any code?

    I really want to understand, why pthread_mutex_init() results are ignored.

  8. #8
    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: Why QMutex.lock() doesn't return any code?

    Quote Originally Posted by googie View Post
    I really want to understand, why pthread_mutex_init() results are ignored.
    Ask @ digia.
    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. QGraphicsScene doesn't return items at point
    By russiankid in forum Qt Programming
    Replies: 0
    Last Post: 7th April 2013, 00:21
  2. QMutex QMutex::Recursive is not a type name
    By Qiieha in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2011, 14:01
  3. Thread mutex lock and return value
    By ^NyAw^ in forum Qt Programming
    Replies: 5
    Last Post: 13th February 2010, 07:32
  4. QMutex seems not to lock()
    By sylvaticus in forum Qt Programming
    Replies: 18
    Last Post: 4th December 2009, 10:39
  5. Replies: 2
    Last Post: 8th August 2006, 15:09

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.