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

  • Home
  • SEARCH
  • 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 6618053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:46:45+00:00 2026-05-25T20:46:45+00:00

Could someone please explain the correct usage for boost::upgrade_lock. The following code results in

  • 0

Could someone please explain the correct usage for boost::upgrade_lock. The following code results in a deadlock

//Global
typedef boost::shared_mutex Mutex;
typedef boost::shared_lock<Mutex> ReadLock;
typedef boost::upgrade_lock<Mutex> UpgradeLock; 
typedef boost::upgrade_to_unique_lock<Mutex> WriteLock;
Mutex sharedMutex;


//Multi threaded reader and writer
{
    ReadLock read(sharedMutex);

    for (int ii = 0; ii < vec.size(); ++ii) {
        Element e = vec[ii];

        if (e.needsUpdating()) {
            UpgradeLock upgrade(sharedMutex);

            WriteLock write(upgrade)

            //Do stuff
        }
    }
}

It doesn’t deadlock if i unlock the read lock with read.unlock() before upgrading. But it seems that this shouldn’t be necessary?

  • 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-05-25T20:46:46+00:00Added an answer on May 25, 2026 at 8:46 pm

    In the boost::shared_mutex class (which implements the UpgradeLockable concept), a single thread should not attempt to acquire both a shared and an upgradeable (or unique) lock. At any time, the UpgradeLockable can have N shared locks held (via lock_shared), and 1 upgradeable lock (via lock_upgrade). The upgradeable lock can request that it become a unique lock, which blocks until it can become the exclusive holder, which requires all shared locks be released. It is impossible to convert from a shared lock to a unique lock, or a shared lock to an upgradeable lock without releasing the shared lock first.

    Note, the upgradeable lock is not exclusive (other shared locks can be held) just that it has special privileges to increase its strength. Unfortunately, multiple upgradeable threads are not allowed at the same time.

    In your case, the same thread is attempting to use lock_shared and lock_upgrade, which will deadlock. You can rewrite it as below, and it won’t deadlock, but it’s still a single point of contention for all of the readers, since only 1 will hold the upgrade lock at a time. In which case, depending on your other functions, the complexity of a shared_mutex might not be necessary. However, if other functions are still acquiring shared locks, then the below will perform as you expect.

    //Multi threaded reader and writer
    {
        // Only 1 thread can pass this.  Other shared locks are also valid
        UpgradeLock read(sharedMutex); 
    
        for (int ii = 0; ii < vec.size(); ++ii) {
            Element e = vec[ii];
    
            if (e.needsUpdating()) {
                // Blocks here until all shareds are released
                WriteLock write(upgrade)
    
                //Do stuff
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please explain to me what the following lines of code do? dynamic
Could someone please explain why this is happening; this following is the code I’ve
could someone please explain why the following code is throwing an error? // JavaScript
Could someone please explain to me why the following query is invalid? I'm running
Could someone please explain the difference in how the 2 snippets of code are
Could someone please explain the Objective-C difference between myString and anotherString in the following
Could someone please explain to me the correct file structure of a Symfony2 MVC
Could someone please explain what is meant by the following ? You must define
Could someone please explain to me why this simple straight forward code isnt working,
I would appreciate if someone could help/explain the following please. I am trying to

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.