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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:51:11+00:00 2026-06-03T20:51:11+00:00

From here: https://stackoverflow.com/a/5524120/462608 If you want to lock several mutex-protected objects from a set

  • 0

From here: https://stackoverflow.com/a/5524120/462608

If you want to lock several mutex-protected objects from a set of such
objects, where the sets could have been built by merging, you can

choose to use per object exactly one mutex, allowing more threads to
work in parallel,

or to use per object one reference to any possibly shared recursive
mutex, to lower the probability of failing to lock all mutexes
together,

or to use per object one comparable reference to any possibly shared
non-recursive mutex, circumventing the intent to lock multiple times.

I just don’t understand the whole quote above. What is he referring to? Please explain in layman’s words.

  • 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-03T20:51:12+00:00Added an answer on June 3, 2026 at 8:51 pm

    Here is my interpretation of the referenced quote. i hope that it’s both understandable and actually matches the intent of the person who wrote that original answer.

    Lets say you have a data structure that needs to be protected by a mutex. You have some options as to how ‘granular’ you can treat the critical sections dealing with those objects. These options can also have an influence on how a thread might need to behave to acquire the locks for multiple objects at the same time:

    • use one mutex per object:

      struct foo {
          mutex mux;
      
          // important data fields...
      };
      

      This has the benefit that threads dealing with different objects will have no contention. If a single thread needs to deal with multiple objects while holding the locks for them (I think this is what’s meant by ‘set merging’), there’s no need for recursive mutexes. However, care does need to be taken to avoid deadlock.

    • have each object refer to a recursive mutex which may be shared with other objects:

      struct foo {
          recursive_mutex* pmux;
      
          // important data fields...
      };
      

      Since two objects may actually be associated with a single mutex, if thread 1 tries to lock object A and thread 2 concurrently tries to lock object B when objects A and B are sharing the same mutex, one of the threads will block until the other thread releases the mutex. Since the mutex is recursive, a single thread may lock multiple objects, even if they share the same mutex. Note that there is still the same caveat about deadlock.

      The (possible) advantage to this scheme over the first is that if a thread has to lock several objects at the same time there’s a certain probability that some of the objects in that set will share a mutex. Once the thread locks one of the objects, in theory the likelihood of blocking when trying to lock the next object is reduced. However, I think in practice it might be rather difficult to prove that you’ll have this benefit unless you can really characterize the locking behavior of your threads and the sets of objects they will be locking (and set up the mutex sharing to mirror that model).

    • The last item in that quote essentially refers to using non-recursive locks in the above scenario. In that case you need to prevent a thread from trying to ‘relock’ a mutex (which, of course, can’t be done with a non-recursive mutex), so the thread will have to somehow compare a lock that it’s about to acquire with the locks that it has already acquired to determine if it should or should not acquire the lock on that object. If more than a few objects are involved, this could become a complicated scenario to ensure that a thread has acquired exactly the right set of locks.

      struct foo {
          mutex* pmux;  // pointer to (possibly shared) non-recursive mutex
      
          // important data fields...
      };
      
      // a set of objects a thread needs to work on in a critical section
      // the objects possibly share non-recursive mutexes
      struct foo* pA;
      struct foo* pB;
      struct foo* pC;
      
      // acquire the necessary locks on all three objects:
      mutex_lock( pA->pmux);
      if (pB->pmux != pA->pmux) mutex_lock( pB->pmux);
      if ((pC->pmux != pA->pmux) && (pC->pmux != pB->p-mux)) mutex_lock( pC->pmux);
      
      // releasing the set of mutexes is similar
      

      Instead of manually acquiring the mutexes inline, it would probably be better to pass them to a function that managed the complexity of making sure that any duplicates were ignored. And as with the previous schemes, avoiding deadlock still needs to be addressed as well.

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

Sidebar

Related Questions

From here: https://stackoverflow.com/a/2485177/462608 For thread-safe accesses to shared data, we need a guarantee that
Here is a quote from https://stackoverflow.com/users/893/greg-hewgill answer to Explain Python's slice notation . Python
Ok so I'm trying to use the custom textview from here: https://stackoverflow.com/a/7875656/938541 Now I
I was just reading from here https://stackoverflow.com/questions/6187904/hard-and-fast-rule-for-include-columns-in-index regarding included columns. n index is typically
I'm having troubles using the facebook binding from here ( https://github.com/mono/monotouch-bindings/tree/master/facebook ) and the
In the comments here -- https://stackoverflow.com/a/9393138/8047 -- I discovered that BOOL has some unexpected
I read the info about the JSTL tag here: https://stackoverflow.com/tags/jstl/info And there's one thing
I know you answered a similar question on ICS here: https://stackoverflow.com/a/8320504 But there is
I posted a detailed question here: https://stackoverflow.com/questions/4252194/need-help-closing-accessing-my-views-in-a-simple-app-included-an-img-of-my-progr but I might have been too confusing.
This is a follow up on a related topic found here https://stackoverflow.com/questions/1987485/conditionally-assign-c-var-as-elegant-as-it-get s if

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.