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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:32:52+00:00 2026-06-11T08:32:52+00:00

void foo ( Bar* bar , void(Bar::*qux)(void) ) { if ( bar ) {

  • 0
void foo ( Bar* bar , void(Bar::*qux)(void) )
{
    if ( bar )
    {
        bar->qux();
    }
}

The problem is:

  1. bar can be deleted after the check by another thread.

  2. I can not add a mutex member to Bar in order to lock it.

Thus I wonder, if I can tell the processor to run this function atomically, and how would I do so? I’ve spent way to much time on Google, but found no understandable manuals…

P.S. Debian, gcc , Boost NOT allowed, C++11 IS allowed.

  • 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-11T08:32:54+00:00Added an answer on June 11, 2026 at 8:32 am

    The concept of atomic methods doesnt exist in C++ like it does in Java, where you can define a method as synchronized. The closest you can get to that in C++ would be to create a ScopedMutex class as follows:

    class ScopedMutex {
    public:
        ScopedMutex(pthread_mutex *m) : theMutex_(m) {pthread_mutex_lock(theMutex_);}
        ~ScopedMutex() { pthread_mutex_unlock(theMutex_); }
        // Add appropriate copy constructors and operator=() to disallow mutex copy
        // Or consider passing in a reference
    private:
        pthread_mutex *theMutex_;
    };
    

    Then use it like this in your function:

    void foo ( Bar* bar , void(Bar::*qux)(void) )
    {
        ScopedMutex m(&aMutex); // This mutex must be defined/initialized elsewhere
    
        if ( bar )
        {
            bar->qux();
        }
    
        // The ScopedMutex goes out of scope when the function does,
        // thus releasing the lock
    }
    

    But this wont do you any good unless you use the same mutex in every other method that uses the bar object.

    Scoped Mutex are especially useful when you have a function with complicated logic where there are several return statements, so you dont have to manually unlock the mutex it will be unlocked when the function goes out of scope.

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

Sidebar

Related Questions

Is following code legal C++ or not? class Foo { class Bar; void HaveADrink(Bar
class Foo { public: void bar(); }; void Foo::bar() { static int n =
When compiling : #include <vector> template<class T> class foo { void bar() { std::vector<T>
So i have some code like this: void foo (int, int); void bar (
If I have the following code: public class Foo { public void Bar() {
Is there a difference between foo and bar: class A { Object __o; void
Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
I have the following code: private String foo; public void setFoo(String bar) { foo
Consider the following code: #include <iostream> struct foo { // (a): void bar() {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); savedInstanceState.putString(foo, bar); } @Override public void

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.