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

The Archive Base Latest Questions

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

class temp { boost::mutex mx; void CriticalCode() { boost::mutex::scoped_lock scoped_lock(mx); //Do Something return; }

  • 0
 class temp
 {
    boost::mutex mx;
    void CriticalCode() {
        boost::mutex::scoped_lock scoped_lock(mx); 
        //Do Something
        return;
    }
 }
  1. If this class is allocated on the heap (temp* T = new temp()), will this be thread safe (for each instance, not all instances together)?

  2. If I make boost::mutex mx -> boost::mutex* mx, and allocate it in the constructor so it will be allocated on the heap, will the code be thread safe also?

  3. If answer to 1 and 2 are no, how can I make each instance thread safe?

  • 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-21T22:45:26+00:00Added an answer on May 21, 2026 at 10:45 pm

    1)if this class is allocated on the heap (temp* T = new temp()) , will this be thread safe (for each instance, not all instances together ?

    Yes. Since mx is not a static member of the class, there will be one lock per instance of the class.

    2)if i make boost::mutex mx -> boost::mutex* mx , and allocate it in the constructor so it will be allocated on the heap , will the code be thread safe also ?

    Yes. But thread safe only on a per-instance basis.

    3)if answer to 1 and 2 are now , how can i make each instance thread safe ?

    The answers are yes so you are fine.

    In case, someone else wonders how to make all instances thread safe with one lock — you can make mx a static variable of the class.

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

Sidebar

Related Questions

If I had a Java class as below, what is the similar Perl representation
Why is it that you can only create a pionter to an inner class
I would like the output to be displayed like this: category1 subcategory1.1 subcategory1.2 category2
I'm a hapless Linux coder trying to get one of my projects working in
There is a weird problem my while loop is not incrementing like its displaying
I have the following code: ViewData.Model = from m in dataModel.Items where m.Loaned ==
When I use hibernate Example API the Id column gets simply ignored, that is
I have a question regarding how to use JSON integration in Android. For example
In C++, i know there are two ways to overload. We can overload it

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.