473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

std::auto_ptr and Unwind Order?

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.
Jul 19 '05 #1
2 2097
"Jamie Burns" <se*****@email.com> wrote in message
news:bo*******************@news.demon.co.uk...
Hi Jamie,
....
| // 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.

First of all, note that it is unnecessary to use an auto_ptr here.
You can simply rely on an automatic (stack-based) variable:
{
rMutexAccess mutexAccess(this->objectMutex);
return visible;
}

| 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.

The returned expression is first evaluated and copied as the
function's result value. Then the function scopes are exited,
and all the stack based objects will be destroyed.
[at least that's my reading of the standard, §6.6,
but this is what happens in practice if you try to test it]

So your code snipped is ok, although I think it has to be simplified...
hth-Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #2
Thanks Ivan!

Of course I don't need auto_ptr! Silly me!

Good to know the variable copy comes first...

Jamie.

"Ivan Vecerina" <pl*****************@ivan.vecerina.com> wrote in message
news:bo**********@newshispeed.ch...
"Jamie Burns" <se*****@email.com> wrote in message
news:bo*******************@news.demon.co.uk...
Hi Jamie,
...
| // 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.

First of all, note that it is unnecessary to use an auto_ptr here.
You can simply rely on an automatic (stack-based) variable:
{
rMutexAccess mutexAccess(this->objectMutex);
return visible;
}

| 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.

The returned expression is first evaluated and copied as the
function's result value. Then the function scopes are exited,
and all the stack based objects will be destroyed.
[at least that's my reading of the standard, §6.6,
but this is what happens in practice if you try to test it]

So your code snipped is ok, although I think it has to be simplified...
hth-Ivan
--
http://ivan.vecerina.com

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: SerGioGio | last post by:
Hello ! When a ref. counted smart pointer (template) class is available (like boost::shared_ptr), is there any (semantic) reason to still use std::auto_ptr rather than this smart pointer ? Or...
9
by: BekTek | last post by:
How do you think? and why?
10
by: ma740988 | last post by:
Part of my confusion here is certainly my ignorance of templates and std::auto_ptr. Two topics, I've perused but need to really _delve_ into. In any event, consider the 'test' source. #...
20
by: Bronek Kozicki | last post by:
Hi Please try this code. I think that it's perfectly legal. However when compiled under MSVC71 stack overflow happens in first line of main, thus second line is never executed. B.
1
by: Guido Forthofer | last post by:
Hello, I convert my VC6 C++ project after VC7 (7.1) and have now an compiler error d:\Programme\Microsoft Visual Studio .NET 2003\Vc7\include\vector(810): error C2558: class...
8
by: Marchello | last post by:
Hi all. I have a class with virtual functions. This class lives in DLL. In main program I use this class by obtaining his pointer (by function that's exports from dll). class CfromDLL { ......
10
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
7
by: j.l.olsson | last post by:
Hello, I am using std::auto_ptr to try to keep ownership of resources straight. However, I became aware that exception objects must provide a copy constructor, which made me uncertain of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.