Results 1 to 6 of 6

Thread: Thread mutex lock and return value

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Thread mutex lock and return value

    Hi,

    I have this situation:
    One thread class that needs to protect a QQueue but also needs to return dequeued value.

    Qt Code:
    1. bool myThread::getResult()
    2. {
    3. m_qMutex.lock();
    4. return m_qQueue.dequeue();
    5. m_qMutex.unlock(); //This will never be executed because of return statment, and will cause a deadlock
    6. }
    To copy to clipboard, switch view to plain text mode 

    So, how can I do this in a correct way. One solution is to get the mutex of the thread from the caller object and lock and unlock there but I want to know if there is a more elegant solution.

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Thread mutex lock and return value

    In general it is a VERY bad and dangerous practice to lock mutex and call other methods/functions, since you never know if another mutex will be locke there too.
    You should only lock local data.

    Like so:
    Qt Code:
    1. reentrantFunction()
    2. {
    3. //good
    4. m_muext.lock()
    5. m_data = some_data;
    6. m_mutex.unlock()
    7.  
    8. //bad
    9. m_mutex.lock();
    10. someFunc();
    11. m_mutex.unlock()
    12. }
    To copy to clipboard, switch view to plain text mode 

    In the case you posted, m_qQueue.dequeue(); has to be re-entrant it self, which means there should the mutex be locked and released and not in getResult(). ( as you can see, another good reason not to lock mutex before function calls)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread mutex lock and return value

    Qt Code:
    1. m_qMutex.lock();
    2. rc = m_qQueue.dequeue();
    3. m_qMutex.unlock();
    4. return rc;
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to pitonyak for this useful post:

    ^NyAw^ (12th February 2010)

  5. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Thread mutex lock and return value

    Hi,

    Thanks,

    pitonyak solution is good.
    Òscar Llarch i Galán

  6. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Thread mutex lock and return value

    indeed, I over looked the fact you are using a "standard" class, basically as a type - sorry for that.
    But be careful in any other cases with locking mutexes before function calls.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. The following user says thank you to high_flyer for this useful post:

    ^NyAw^ (12th February 2010)

  8. #6
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread mutex lock and return value

    Also possible:
    Qt Code:
    1. QMutexLocker locker(&m_qMutex);
    2. return m_qQueue.dequeue();
    To copy to clipboard, switch view to plain text mode 
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. Lock a file
    By euthymos in forum Qt Programming
    Replies: 22
    Last Post: 27th December 2010, 23:29
  2. sqlite read lock.
    By gilgm in forum Qt Programming
    Replies: 6
    Last Post: 18th June 2010, 05:58
  3. SQLite + journal + lock
    By NoRulez in forum Qt Programming
    Replies: 4
    Last Post: 14th December 2009, 08:25
  4. QMutex seems not to lock()
    By sylvaticus in forum Qt Programming
    Replies: 18
    Last Post: 4th December 2009, 10:39
  5. Qthread mutex
    By dognzhe in forum Qt Programming
    Replies: 5
    Last Post: 18th May 2009, 05:46

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.