std::auto_ptr and Unwind Order?

Discussion in 'C++' started by Jamie Burns, Nov 7, 2003.

  1. Jamie Burns

    Jamie Burns Guest

    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.
    Jamie Burns, Nov 7, 2003
    #1
    1. Advertising

  2. "Jamie Burns" <> wrote in message
    news:bogkui$1mn$1$...
    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
    Ivan Vecerina, Nov 7, 2003
    #2
    1. Advertising

  3. Jamie Burns

    Jamie Burns Guest

    Thanks Ivan!

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

    Good to know the variable copy comes first...

    Jamie.

    "Ivan Vecerina" <> wrote in message
    news:bogm6j$i6l$...
    > "Jamie Burns" <> wrote in message
    > news:bogkui$1mn$1$...
    > 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
    >
    >
    Jamie Burns, Nov 7, 2003
    #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. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,533
    Dave Rahardja
    Jan 11, 2005
  2. v4vijayakumar

    enabling unwind semantics

    v4vijayakumar, May 3, 2006, in forum: C Programming
    Replies:
    17
    Views:
    1,342
    Alf P. Steinbach
    May 4, 2006
  3. v4vijayakumar

    enabling unwind semantics

    v4vijayakumar, May 3, 2006, in forum: C Programming
    Replies:
    0
    Views:
    312
    v4vijayakumar
    May 3, 2006
  4. v4vijayakumar

    enabling unwind semantics

    v4vijayakumar, May 3, 2006, in forum: C++
    Replies:
    27
    Views:
    857
    Marcus Kwok
    May 4, 2006
  5. Sousuke
    Replies:
    9
    Views:
    1,126
    Bart van Ingen Schenau
    Mar 16, 2010
Loading...

Share This Page