
Having looked at Boost threads and shmem 0.9 recently, I'm trying to get to grips with a 'scoped lock' ONLY design rationale having laboured under the posix threads APIs for a while. While I appreciate the reasons, I am a little confused ... All the examples I've seen show scoped locks being constructed local to a function and then destroyed automatically at the end of function scope. I would like my own lock manager object to manage a group of locks and allow me to leave one or more mutexes locked across function calls until the lock manager object goes out of scope and releases any mutexes still flagged as locked. Is this possible? e.g. a vector/array/deque of pointers to scoped_timer_locks each relating to its own mutex and constructed as unlocked. I understand that leaving mutexes locked is not a great idea! The application does think it knows what it is doing. Paul

On 4/18/06, Paul Baxter
All the examples I've seen show scoped locks being constructed local to a function and then destroyed automatically at the end of function scope.
I would like my own lock manager object to manage a group of locks and allow me to leave one or more mutexes locked across function calls until the lock manager object goes out of scope and releases any mutexes still flagged as locked.
As usual when you want fancy scope control, you can use a pointer. One of the smart pointers to the lock would work nicely. IIRC you can also explicitly *un*lock scoped locks, so you might be able to have your manager lock them all on construction then unlock as nessesary. ~ SWMc

On 4/18/06, me22
As usual when you want fancy scope control, you can use a pointer. One of the smart pointers to the lock would work nicely.
Or how about making a smart pointer itself manage the lock? I especially like Alexendrescu's LockingPtr, but rather than go with his OOTB implementation, factor out the mutex into a wrapper class so you end up with class implementors not caring about multi-threadedness and clients explicitly choosing whether or not they want thread safety. For example, something like: Lockable< MyClass > myClass; LockingPtr< MyClass > myClassPtr( myClass ); Noel

If you decide to use shared_ptr implementation from boost, then you do not need scoped_lock class at all... All you need is a custom deleter class, which will unlock the object when the shared_ptr is destroyed or create a copy constructible mutex lock. Take a look at the following sample, or better study all the samples: http://www.boost.org/libs/smart_ptr/sp_techniques.html#as_lock With Kind Regards, Ovanes Markarian On Wed, April 19, 2006 3:23, me22 said:
On 4/18/06, Paul Baxter
wrote: All the examples I've seen show scoped locks being constructed local to a function and then destroyed automatically at the end of function scope.
I would like my own lock manager object to manage a group of locks and allow me to leave one or more mutexes locked across function calls until the lock manager object goes out of scope and releases any mutexes still flagged as locked.
As usual when you want fancy scope control, you can use a pointer. One of the smart pointers to the lock would work nicely.
IIRC you can also explicitly *un*lock scoped locks, so you might be able to have your manager lock them all on construction then unlock as nessesary.
~ SWMc
participants (4)
-
me22
-
Noel Yap
-
Ovanes Markarian
-
Paul Baxter