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 9032073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:53:29+00:00 2026-06-16T07:53:29+00:00

In multithreading (2 thread) program, I have this code: while(-1) { m.lock(); (…) m.unlock();

  • 0

In multithreading (2 thread) program, I have this code:

while(-1)
{
    m.lock();

    (...)

    m.unlock();
}

m is a mutex (in my case a c++11 std::mutex, but I think it’doesn’t change if I use different library).

Assuming that the first thread owns the mutex and it’s done something in (...) part. The second thread tried to acquire the mutex, but it’s waiting that the first thread release m.

The question is: when thread 1 ends it’s (...) execution and unlocks the mutex, can we be sure that thread 2 acquires the mutex or thread 1 can re-acquire again the mutex before thread 2, leaving it stucked in lock()?

  • 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-16T07:53:30+00:00Added an answer on June 16, 2026 at 7:53 am

    If both threads are equal priority, there is no such guarantee by standard mutex implementations. Some OS’s have a lis of “who’s waiting”, and will pick the “longest waiting” when you release something, but that is an implementation detail, not something you can reliably depend on.

    And imagine that you have two threads, each running something like this:

    m.lock();
    
    (...)
    
    m.unlock();
    (...)    // Clearly not the same code as above (...)
    m.lock();
    
    (...)    // Some other code that needs locking against updates. 
    
    m.unlock();
    

    Would you want the above code to switch thread on the second lock, every time?

    By the way, if both threads run with lock for the entire loop, what is the point of a lock?

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

Sidebar

Related Questions

I am using ThreadPoolExecutor in my multithreading program, I want each thread should have
I have created a program in android for multithreading. When I hit one of
I wrote a program that employs multithreading for parallel computing. I have verified that
I have this code that was running for about 2 hours. After that the
I found this tutorial on web regarding multithreading and download code to try on
Have a doubt in multithreading. Following is my main program to access a file,
I have created this little program to calculate pi using probability and ratios. In
I am digging into multithreading, found some good tutorials but I have some remaining
So I have worked with Multithreading in small application. Create a new thread here,
Emulating multithreading by loading a DLL multiple times (it's designed for this). Since LoadLibrary()

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.