void foo ( Bar* bar , void(Bar::*qux)(void) )
{
if ( bar )
{
bar->qux();
}
}
The problem is:
-
barcan be deleted after the check by another thread. -
I can not add a mutex member to
Barin 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.
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:
Then use it like this in your function:
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.