Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Order that things happen when returning from a function.

Reply
Thread Tools

Order that things happen when returning from a function.

 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      05-27-2008
Let's say I have something like this, a class that wraps a "Mutex",
locks it on construction and unlocks it when it's destroyed (for
example):

class AutoMutex {
public:
AutoMutex (Mutex &) ... ;
~AutoMutex () ... ;
};

Now let's say I use it in a function that returns a copy of another
complex object:

class Something { ... };

Mutex TheMutex;
Something TheThing;

Something function () {
AutoMutex m(TheMutex);
return TheThing;
}

If I am using, in that example, "TheMutex" to protect access to
"TheThing", will "TheThing" be accessed before or after "m" goes out
of scope and unlocks the mutex?

Thanks,
Jason
 
Reply With Quote
 
 
 
 
Helge Kruse
Guest
Posts: n/a
 
      05-27-2008
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Let's say I have something like this, a class that wraps a "Mutex",
> locks it on construction and unlocks it when it's destroyed (for
> example):

[...]
>
> class Something { ... };
>
> Mutex TheMutex;
> Something TheThing;
>
> Something function () {
> AutoMutex m(TheMutex);
> return TheThing;
> }
>
> If I am using, in that example, "TheMutex" to protect access to
> "TheThing", will "TheThing" be accessed before or after "m" goes out
> of scope and unlocks the mutex?


The AutoMutex destructor is called after TheThing is accessed. If TheThing
is an struct or class, the assignemnt to the caller's object is called
before TheMutex is unlocked. If TheThing is a scalar (hold in a register),
the value is saved before calling Mutex destuctor.

/Helge


 
Reply With Quote
 
 
 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      05-27-2008
On May 27, 7:38 am, "Helge Kruse" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> wrote in message
> > If I am using, in that example, "TheMutex" to protect access to
> > "TheThing", will "TheThing" be accessed before or after "m" goes out
> > of scope and unlocks the mutex?

>
> The AutoMutex destructor is called after TheThing is accessed. If TheThing
> is an struct or class, the assignemnt to the caller's object is called
> before TheMutex is unlocked. If TheThing is a scalar (hold in a register),
> the value is saved before calling Mutex destuctor.


Thanks.

Jason
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Amazing things as they happen iL_weReo Computer Support 0 11-11-2009 08:34 PM
Reference lets a function to return a variable . But strange things happen :(:( karthikbalaguru C++ 10 09-05-2007 03:52 PM
Wired things happen with g++-3.3.5 PengYu.UT@gmail.com C++ 7 10-15-2005 03:55 PM
When Bad things Happen to Good Lenses Charles Digital Photography 13 08-26-2005 11:44 AM



Advertisments