Order that things happen when returning from a function.

Discussion in 'C++' started by jason.cipriani@gmail.com, May 27, 2008.

  1. Guest

    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
    , May 27, 2008
    #1
    1. Advertising

  2. Helge Kruse Guest

    <> wrote in message
    news:...
    > 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
    Helge Kruse, May 27, 2008
    #2
    1. Advertising

  3. Guest

    On May 27, 7:38 am, "Helge Kruse" <> wrote:
    > <> 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
    , May 27, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=

    vs2005 publish website doing bad things, bad things

    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=, Oct 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    589
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Oct 25, 2006
  2. Replies:
    7
    Views:
    357
    Branimir Maksimovic
    Oct 15, 2005
  3. karthikbalaguru
    Replies:
    10
    Views:
    519
    terminator
    Sep 5, 2007
  4. r0g
    Replies:
    2
    Views:
    239
  5. Robbie Hatley

    print prints things in strange order.

    Robbie Hatley, Oct 7, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    110
    Robbie Hatley
    Oct 9, 2005
Loading...

Share This Page