开发者

C++: Mutex and deallocation

I am writing a small program as part of a University course. In this program, I have a global Boost Mutex which helps me to coordinate two threads.

In my small library, I have to write开发者_如何学Python a cleanup function that, when invoked, literally cleans up after itself.

What I am asking now is: if I create a Mutex, am I supposed to deallocate it as well when I do not need it anymore?

The code I use is simply

boost::mutex mymutex;

Thanks a lot


Boost mutexes appear to be written to perform all their cleanup in their destructors. If you want to ready that same mutex for resuse, probably you'd want to call .unlock() on it once.

If you really feel the need to manually get rid of it, I suppose you could make it a pointer and create it with a new. That way you can manually control running of its destructor in your cleanup routine by calling delete on the pointer. However, pointers are kind of error-prone, so using one just so you can show it manually being cleaned up (instead of having it automatically happen when the object goes out of scope at the end of your program) is stoopid. If you get docked a couple of points for not doing that, I'd consider it a small price to pay for designing things right instead.

If you are really worried about losing points for not manually cleaning up your automatically destructed resources, if I were you I'd go ask my instructor what I need to do. School ain't free (unlike SO), and such consulting is part of what you are paying them all that money for. Might as well get some value for it.


It will be deallocated when running out of scope which is defined by the curly braces. You should be careful to unlock the mutex.


Nope. Objects in C++ clean up after themselves. You would only need to clean up the mutex if you had allocated it dynamically with new.


In the clean up function you might need to call release/reset methods of the boost::mutex object to make sure you are not leaving it in signalled state.


@Danilo: I'm pretty sure the course doesn't require you to use boost, and the teach expects you to use the pthreads API directly.

In that case you'd want to call pthread_mutex_init and pthread_mutex_destroy, which boost does automatically for you


When you define an object (as opposed to a pointer), such as in --

boost::mutex mymutex;

-- C++ will call the object's destructor when the object goes out of scope.

What you need to do now is check the documentation of boost::mutex on what it'll do when it's d'tor is called. However, you can be very sure that any class inside boost will clean up its resources in it's dtor (that's what a dtor is there for after all). What you do not know without checking the docs is what the Mutex's behaviour wrt. it's lock() function is when the dtor is being called.

Since you have a global object, its dtor will be called after main() returns which should be fine as far as resource deallocation goes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜