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

I have a object cache class like this: #include boost/thread/mutex.hpp #include boost/unordered_map.hpp template <typename
I've defined some signal: typedef boost::signals2::signal<void (int temp)> SomeSig; typedef SomeSig::slot_type SomeSigType; I have
class A : IFoo { } ... A[] arrayOfA = new A[10]; if(arrayOfA is
I'm wondering if this is the right approach to writing a thread-safe queue in
I have some class which uses boost singleton. It calls some function from own
I have a class which contains a few boost::numeric::ublas::matrix's within it. I would like
Lets say I have this XML file: <weather> <temp>24.0</temp> <current-condition iconUrl=http://....>Sunny</current-condition> </weather> I'm trying
template<class Container> void BlitSurface::ExtractFrames(Container & output, int frame_width, int frame_height, int frames_per_row, int frames_per_column,
class Temp { public : Temp(X& x): x_(x) {} Temp(X& x, Y& y) :
For example I have a class named Temp then I assigned values to 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.