[Locks/Mutex] choices between types
Hello This post may not be well suited to this mailing list. I have some questions about the lockable concept provided in boost. There are many "lock" types, and many "mutex" types. My main question is then : when should I chose a given lock type rather a given mutex type ? Given the fact that some of lock types are not thread-safe, if only one thread needs locks, why should I use one of these lock types and why shouldnt I use some mutex type ? Finally, what were the goals of providing so much different ways to lock/unlock resources ? (I may not be aware of all the concepts related to secure resource access) Since the "lock" et "mutex" types provide ways to lock/unlock access to resources, why are not these types defined under the same name ? Are the concepts they implements much more different than I think ? Thanks Axel
Hi Alex, A mutex object contains all the data structures for locking. A lock object locks the given mutex object in the constructor and unlocks it in its destructor. Example: class ThreadSafeSomething { private: boost::mutex mutex; public: void doSomething() { boost::mutex::scoped_lock(mutex); something(); } // mutex is unlocked here }; If there would be no lock object you would have to write something like (welcome to java world): class ThreadSafeSomething { private: my::special::mutex mutex; public: void doSomething() { mutex.lock(); something(); mutex.unlock(); } }; Final question: What happens when an exception is thrown in something()? Kind regards, Markus Am Mittwoch, den 01.07.2009, 17:08 +0200 schrieb Axel:
Hello
This post may not be well suited to this mailing list. I have some questions about the lockable concept provided in boost. There are many "lock" types, and many "mutex" types. My main question is then : when should I chose a given lock type rather a given mutex type ? Given the fact that some of lock types are not thread-safe, if only one thread needs locks, why should I use one of these lock types and why shouldnt I use some mutex type ?
Finally, what were the goals of providing so much different ways to lock/unlock resources ? (I may not be aware of all the concepts related to secure resource access)
Since the "lock" et "mutex" types provide ways to lock/unlock access to resources, why are not these types defined under the same name ? Are the concepts they implements much more different than I think ?
Thanks Axel _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Since the "lock" et "mutex" types provide ways to lock/unlock access to resources, why are not these types defined under the same name ? Are the concepts they implements much more different than I think ?
As Markus mentioned, lock objects are just raii-style helpers for exception safe locking. As for multiple mutex types, they stand for different approaches to the resource sharing problem. If you just want to always lock your resource exclusively, use simple "mutex" class; if you prefer to allow simultaneous shared access as long as there're readers only, use shared_mutex; and if you'd like to be able to lock the mutex several times from the same thread, use recursive_mutex (like this: { mutex_.lock(); mutex_.lock(); /*....*/ mutex_.unlock(); mutex_.unlock(); }
participants (3)
-
Axel
-
Igor R
-
Markus Bernhardt