J
Jamie Burns
Hello,
I am trying to write some threadsafe code. I am using mutexes to restrict
access to certain class members. Consider the method below:
// begin code nippet
bool rControl::getVisibility() {
{
std::auto_ptr<rMutexAccess> mutexAccess(new
rMutexAccess(this->objectMutex));
// get visibility
return visible;
}
}
// end code nippet
So, basically, I am creating the mutex using an auto_ptr to give myself a
guarantee that the mutex will unlock (it unlocks on destruction of the
rMutexAccess) no matter what happens in the code.
My question is this: in the above code, which happens first? A) Does the
member "visible" get copied to be returned, after which the mutex destructs,
or B) The mutex destructs, and then the member "visible" get copied to be
returned.
Can you see what I am getting at? In normal code it is fine, I can see
clearly what will happen, but with a "return" statement inside the protected
nest { ... } I am not sure what happens.
Thanks!
Jamie Burns.
I am trying to write some threadsafe code. I am using mutexes to restrict
access to certain class members. Consider the method below:
// begin code nippet
bool rControl::getVisibility() {
{
std::auto_ptr<rMutexAccess> mutexAccess(new
rMutexAccess(this->objectMutex));
// get visibility
return visible;
}
}
// end code nippet
So, basically, I am creating the mutex using an auto_ptr to give myself a
guarantee that the mutex will unlock (it unlocks on destruction of the
rMutexAccess) no matter what happens in the code.
My question is this: in the above code, which happens first? A) Does the
member "visible" get copied to be returned, after which the mutex destructs,
or B) The mutex destructs, and then the member "visible" get copied to be
returned.
Can you see what I am getting at? In normal code it is fine, I can see
clearly what will happen, but with a "return" statement inside the protected
nest { ... } I am not sure what happens.
Thanks!
Jamie Burns.