By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,787 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

Query: Will a MutexGuard object release before a function return value is copied?

P: n/a
Query: Will a MutexGuard object release before a function return value
is copied?

Consider the C++ code:

class MutexGuard //A familiar sort of class for making mutexes
exception-safe
{
. . .
~MutexGuard(); //releases the mutex when the stack pops
}

int MyClass::increment(int value) //thread-safe
{
MutexGuard myguard(m_mutex); //lock the private member data
m_privatemember = value;
++m_privatemember;
return m_privatemember;
}

When the function completes, the new value of m_privatemember is
copied for
return-by-value, myguard is popped off the stack, its destructor runs,
and the mutex is released.

Is there any chance that the mutex is released before m_privatemember
is copied for return-by-value? If that happened, another thread could
nip in and change the value before it is copied.

Tropos
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 19 Dec 2003 09:01:59 -0800, tr**********@hotmail.com (Tropos)
wrote:
Query: Will a MutexGuard object release before a function return value
is copied?


I suspect not, but try comp.programming.threads (where threading
issues are discussed for languages including C++) for a definitive
answer.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.