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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:33:08+00:00 2026-05-17T17:33:08+00:00

I have a thread pool that is using shared mutexes from the boost library.

  • 0

I have a thread pool that is using shared mutexes from the boost library.

While the answers to my other question were helpful,
Example of how to use boost upgradeable mutexes

What I have realised that what I actually need is not to block if a shared lock or upgrade lock could not be obtained. Unfortunately, the boost docs are lacking any examples of those in use.

Could someone please point me to or provide an example of specifically the shared_lock being used in this way.

i.e.

boost:shared_mutex mutex;

void thread()
{
    // try to obtain a scoped shared lock
    // How do I do that?
}

void thread2()
{
   // try to obtain a scoped upgrade lock 
   // and then a scoped unique 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-05-17T17:33:08+00:00Added an answer on May 17, 2026 at 5:33 pm

    The answer seems to be that you can provide boost:try_to_lock as a parameter to several of these scoped locks.

    e.g.

    boost::shared_mutex mutex;
    
    // The reader version
    boost::shared_lock<boost::shared_mutex> lock(mutex, boost::try_to_lock);
    if (lock){
      // We have obtained a shared lock
    }
    
    // Writer version
    boost::upgrade_lock<boost::shared_mutex> write_lock(mutex, boost::try_to_lock);
    if (write_lock){
      boost::upgrade_to_unique_lock<boost::shared_mutex> unique_lock(write_lock);
      // exclusive access now obtained.
    }
    

    EDIT:
    I also found by experimentation that upgrade_to_unique_lock will fail if you don’t have the upgrade lock. You can also do this:

    boost::upgrade_to_unique_lock<boost::shared_mutex> unique_lock(write_lock);
    if (unique_lock){
      // we are the only thread in here... safe to do stuff to our shared resource
    }
    
    // If you need to downgrade then you can also call
    unique_lock.release();
    
    // And if you want to release the upgrade lock as well (since only one thread can have upgraded status at a time)
    write_lock.unlock().
    

    Note: You have to call release followed by unlock or you’ll get an locking exception thrown.
    You can of course just let unique_lock and write_lock go out of scope thereby releasing the locks, although I’ve found that sometimes you want to release it earlier and you should spend minimal time in that state.

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

Sidebar

Related Questions

I have a thread from a thread pool in a service that does quite
I have a multithreaded application that runs using a custom thread pool class. The
I have a UDP based application that is implemented using a thread pool. Messages
I have a custom thread pool class, that creates some threads that each wait
I have some code that makes heavy use of a thread pool, which I
I have a Java application that has a fixed thread pool of fifteen, the
I have an existing java/scala application using a global thread pool. I would like
I'm allocating my pthread thread-specific data from a fixed-size global pool that's controlled by
I have a C# class, MyCommand, that is run using a backgroundWorker and thread
In my application I have a user control that do async operations using thread

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.