which one run first, destructor or return method

This is a discussion on which one run first, destructor or return method within the C++ Programming forums, part of the General Programming Boards category; For example i have a class mutex : Code: class Mutex { pthread_mutex_t & mMutex; public: Mutex(pthread_mutex_t & m):mMutex(m) { ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    17

    which one run first, destructor or return method

    For example i have a class mutex :
    Code:
    class Mutex
    {
    	pthread_mutex_t & mMutex;
    public:
    	Mutex(pthread_mutex_t & m):mMutex(m)
    	{
    		pthread_mutex_lock(&mMutex);
    	}
    	~Mutex()
    	{
    		pthread_mutex_unlock(&mMutex);
    	}
    };
    
    public void Test(){
            Mutex(mx);
            return a;
    }
    In function Test(), is a will be safely return? i mean will the method got the a value before the mutex destroyed?

    Thanks

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    I'm not sure if this is a question of auto object management or of thread behavior. It would seem to depend on 'a', no?
    Code:
    #include <iostream>
    #include <string>
    
    class foo
    {
       public:
           foo(std::string & s_) : s(s_) {}
           ~foo() { s = "destroyed"; }
       private:
           std::string & s;
    };
    
    std::string & test(std::string & s)
    {
       foo bar(s);
       return s;
    }
    
    int main()
    {
          std::string s("Not destoyed");
          std::cout << test(s) << std::endl;
    }
    *edit* ahh is the instance of Mutex called 'a'?
    Last edited by CodeMonkey; 01-09-2009 at 01:45 AM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    17
    *edit* ahh is the instance of Mutex called 'a'?
    i dont understand what did u mean >.<

    the 'a' variable is any variable that would like to be synchronized.
    usually we may write it like this :

    Code:
    public int Test(){
        pthread_mutex_lock(&mutex);
        int res = a; //a is class' variable
        pthread_mutex_unlock(&mutex);
       
       return a;
    }
    Will it be the same and safe with code below?

    Code:
    public int Test(){
            Mutex(mutex);
            return a;
    }
    Last edited by sleith; 01-09-2009 at 01:55 AM.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    I don't know enough to say. Sorry
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    17
    that's ok, i appreciate u, thx

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I would think that objects go out of scope after the return statement. Otherwise if you have

    Code:
    X foo()
    { 
        X a, b;
        //...
        return a + b;
    }
    then it would be quite strange if a and b were already destructed.

    I'm no good in threading, but shouldn't it actually be the caller of the function who ensures that things are locked while it is assigning or using the return value of Test()?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    That should work as intended, but it doesn't seem like a good idea. You'd usually want more control over locking/unlocking and using it from the calling functions rather than lazily relying on deconstructors for the entire scope. Of course it could be put into it's own scope as well.

    Code:
    public int Test()
    {
            {
                    Mutex(mutex);
    
                    //do something here
            }
    
            return a;
    }
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    There's actually an implementation of this in Boost (boost::mutex::scoped_lock). I have just about no experience in concurrent programming, but I thought it was a pretty neat idea.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dae View Post
    That should work as intended, but it doesn't seem like a good idea. You'd usually want more control over locking/unlocking and using it from the calling functions rather than lazily relying on deconstructors for the entire scope. Of course it could be put into it's own scope as well.

    Code:
    public int Test()
    {
            {
                    Mutex(mutex);
    
                    //do something here
            }
    
            return a;
    }
    Nio, it's actually quite common to make locking ojects that unlock on destruction - it is a good way to avoid deadlocks caused by forgetting to unlock!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The only question is whether the access is before or after the destruction.

    The C++ standard does say something about this, I'm pretty sure, (something along the lines of "the argument to the return statement is evaluated completely before the destructors run"), but having no access to it right now, I can't verify.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Bjarne Stroustrup says: 10.4.4: A named automatic object, which is created each time its declaration is encountered in the execution of the program and destroyed each time the program exits the block in which it occurs. In this case, exiting the block happens when it has gone past the return, technically speaking.

    Also, as examples have described above, if you return a + b, where a and b are local objects which gets destroyed when we leave the function, a + b would not work correctly if it's not evaluated BEFORE destruction - and this type of code is far from unusual.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The problem I see is that the mutex isn't a named object. Being an anonymous object might change the behavior a bit. For example, there is no reason that the object couldn't be destroyed right after it was created as there is no further use for the object.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Thantos View Post
    The problem I see is that the mutex isn't a named object. Being an anonymous object might change the behavior a bit. For example, there is no reason that the object couldn't be destroyed right after it was created as there is no further use for the object.
    That is a good point. It should have a name.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Oh yes, I noticed that and then forgot to post about it. The temporary must be destroyed at the end of the statement that creates it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by CornedBee View Post
    Oh yes, I noticed that and then forgot to post about it. The temporary must be destroyed at the end of the statement that creates it.
    Unless the temporary is assigned to a const-reference.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Figure out how method was called?
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2009, 10:43 AM
  2. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  3. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 08:01 AM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 08:51 AM
  5. Delegate Calling a method that Calls a delegate.
    By xddxogm3 in forum C# Programming
    Replies: 2
    Last Post: 05-05-2008, 01:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21