Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:48:19+00:00 2026-06-04T16:48:19+00:00

In Qt documentation about QMutex it is said: (…) When you call lock() in

  • 0

In Qt documentation about QMutex it is said:

(…) When you call lock() in a thread, other threads that try to call lock() in the same place will block until the thread that got the lock calls unlock(). A non-blocking alternative to lock() is tryLock(). (…)

I have been using this code many times:

QMutex mutex;<br>
while( !mutex.tryLock() );

Can someone explain me how this tryLock() method is built that while() loop won’t hang whole program?

Two threads shares one QMutex and act as a communication FIFO – when one thread is sending data, and another data is scheduled to the second thread, that thread is waiting for the first thread to complete. Communication comply with Modbus standard – send1-receive1, send2-receive2.

You cannot make parallel send and receive. So always one thread is active, the rest is waiting.

QMutex mutex; 
thread1() { 
    while( !mutex.tryLock() )
        ; 
    doThread1Job(); 
} 

thread2() { 
    while( !mutex.tryLock() )
        ; 
    doThread2Job(); 
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:48:20+00:00Added an answer on June 4, 2026 at 4:48 pm

    If you’re spinning on tryLock() in the GUI thread, then of course it will block your user interface. A tryLock() does not spin an event loop or anything like that. In your case, the tryLock() loop is same as just calling lock().

    The non-blocking alternative to tryLock() should be used as follows: if it fails, you wait and try again. You set up a singleShot QTimer to fire your retry slot.

    In any case, it’d probably be easier to send a Qt signal to the waiting thread from the working thread, instead of implementing your own means of synchronization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read all documentation about thread-safe types and the lock statement, but I am
I'm reading the documentation about DetachedCriteria. The documentation clearly shows that setting an alias
I look that nice documentation about Ext JS 4 applciation architecture. It tells clearly
There's no text in the documentation about what that means, but it sounds very
I have been reading documentation about the new features that IIS 7.5 offers regarding
The documentation about List mention that itemTpl follows the XTemplate syntax. I would like
Apple does say in the documentation about the fact that they do not want
I just saw that there is no so much documentation about sfGuardAuth groups and
I've read the documentation about Bound Services , where it is shown that you
Reading over the documentation about include_once it basically says that the file in question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.