question about threading library
I want to use the boost threading library in an application. Looking through the documentation, I have a questions. a) in the examples I find "boost::mutex::scoped_lock" but I don't find this in the documentation. It looks like this is called "lock_guard". What should I be doing here. b) Its not clear to me how unique_lock(Lockable l) is different than using lock_gard(..) or scoped_lock. Robert Ramey
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 06 November 2008 14:44 pm, Robert Ramey wrote:
I want to use the boost threading library in an application.
Looking through the documentation, I have a questions.
a) in the examples I find "boost::mutex::scoped_lock" but I don't find this in the documentation. It looks like this is called "lock_guard". What should I be doing here.
I believe the mutex::scoped_lock, etc. are only there for backward compatibility with the older Boost.Thread API.
b) Its not clear to me how unique_lock(Lockable l) is different than using lock_gard(..) or scoped_lock.
unique_lock is just more featureful/complex than lock_guard. You can't do a "try lock" or "timed lock" with a lock_guard, for example. Personally, I just use unique_lock. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFJE1lH5vihyNWuA4URAiGvAJ9ksLtJCobAJjy9SazzVi0/NQemvwCffrmX DH0IdB3Qy6LY4LXJ0FwWNsg= =b+Te -----END PGP SIGNATURE-----
Hi! Here are simple examples: 1. boost::mutex m; // non-thread safe commands { // non-thread safe commands boost::mutex::scoped_lock L (m); // thread safe commands } // on exiting scope the L is destroyed and so mutex _m_ is AUTO-unlocked. very convenient! // non-thread safe commands 2. boost::shared_mutex shm; can be locked with boost::unique_lockboost::shared_mutex ul (shm) or boost::shared_lockboost::shared_mutex ul (shm) so this implements single_writer/multiple_readers concept Hope this will help you to understand threads :)
"Robert Ramey"
I want to use the boost threading library in an application.
Looking through the documentation, I have a questions.
a) in the examples I find "boost::mutex::scoped_lock" but I don't find this in the documentation. It looks like this is called "lock_guard". What should I be doing here.
boost::mutex::scoped_lock is provided for backwards compatibility. It is a typedef for boost::unique_lockboost::mutex. I suggest using lock_guard unless you need backwards compatibility with boost 1.34 or earlier, or need the flexibility provided by unique_lock.
b) Its not clear to me how unique_lock(Lockable l) is different than using lock_gard(..) or scoped_lock.
lock_guard is a lightweight wrapper that *always* owns the lock. It is not movable, and doesn't support try_lock, timed locks or unlock/release. unique_lock is a more fully-featured lock owner. It supports the full Lockable interface (including try_lock and timed_lock), so you can pass it to boost::lock. Ownership can be transferred between unique_lock instances, and a given instance may or may not have an associated mutex, and may or may not own the lock on its mutex. You can also use unique_lock with condition variables, which you can't do with lock_guard. scoped_lock is just a typedef to unique_lock in most cases. Anthony -- Anthony Williams Author of C++ Concurrency in Action | http://www.manning.com/williams Custom Software Development | http://www.justsoftwaresolutions.co.uk Just Software Solutions Ltd, Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK
Thanks for this information. I appreciate the effort you've put into this, and I have a lot of confidence in the library. But I feel it would be more useful to me and others if it were it to have a lot more tutorial type documentation and examples. Robert Ramey Anthony Williams wrote:
"Robert Ramey"
writes: I want to use the boost threading library in an application.
Looking through the documentation, I have a questions.
a) in the examples I find "boost::mutex::scoped_lock" but I don't find this in the documentation. It looks like this is called "lock_guard". What should I be doing here.
boost::mutex::scoped_lock is provided for backwards compatibility. It is a typedef for boost::unique_lockboost::mutex.
I suggest using lock_guard unless you need backwards compatibility with boost 1.34 or earlier, or need the flexibility provided by unique_lock.
b) Its not clear to me how unique_lock(Lockable l) is different than using lock_gard(..) or scoped_lock.
lock_guard is a lightweight wrapper that *always* owns the lock. It is not movable, and doesn't support try_lock, timed locks or unlock/release.
unique_lock is a more fully-featured lock owner. It supports the full Lockable interface (including try_lock and timed_lock), so you can pass it to boost::lock. Ownership can be transferred between unique_lock instances, and a given instance may or may not have an associated mutex, and may or may not own the lock on its mutex.
You can also use unique_lock with condition variables, which you can't do with lock_guard.
scoped_lock is just a typedef to unique_lock in most cases.
Anthony
"Robert Ramey"
Thanks for this information.
You're welcome.
I appreciate the effort you've put into this, and I have a lot of confidence in the library. But I feel it would be more useful to me and others if it were it to have a lot more tutorial type documentation and examples.
You're not the only one who's mentioned that. I'll write some tutorials when I get a chance. Anthony -- Anthony Williams Author of C++ Concurrency in Action | http://www.manning.com/williams Custom Software Development | http://www.justsoftwaresolutions.co.uk Just Software Solutions Ltd, Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK
participants (5)
-
Anthony Williams
-
Frank Mori Hess
-
Robert Ramey
-
Rodrigo Madera
-
Roman Shmelev