4

Can I have pointers to user defined functions as the template type for atomic variables ? Something like this

class A
{
int d;
public:
 void foo() { cout<<"Hellow wolrd!"; }
};

int main()
{
atomic<A*> ptrA;
//now how to call A::foo() from ptrA ?

}
2
  • You need to have an A object first... Jun 20, 2016 at 10:33
  • Operators are overriden so you can simply use : (*ptrA).foo()
    – Omar
    Jun 20, 2016 at 10:33

1 Answer 1

3

You have two ways to call the method:

Method 1:

(*ptrA).foo();

Method 2

A* a = ptrA.load();

a->foo();

I don't know anything about your multi-threaded scenario to advise how best to avoid any pitfalls as you've not provided much information but the second way obviously allows you to guard against certain outcomes. Note also that the load method on std::atomic can accept a number of different memory ordering constraints.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.