Boost logo

Boost :

From: William E. Kempf (williamkempf_at_[hidden])
Date: 2002-05-17 09:03:10


----- Original Message -----
From: "Matthew Wilson" <MWilson_at_[hidden]>

> This seems nice, but is there a practical need for the lock() and unlock()
methods outside of their implementation in ctor/dtor? I've worked on various
multithreaded projects on a number of platforms and never seen the need for
anything more than
>
> template <typename T>
> class LockScope
> {
> LockScope(T &t)
> : m_t(t)
> {
> t_specific_lock_call(m_t);
> }
> ~LockScope()
> {
> t_specific_unlock_call(m_t);
> }
>
> ...
> };
>
> using as
> ...
> { // Some scope
> LockScope<Mutex> lock(mx);
> some_operation_guarded_by_mutex();
> }
> ...
> Other people may have seen a need for more, but if not, let's KISS it.

Yes, there *IS* a need, and I think I clearly showed them with the two use
case scenarios given. Both are patterns that, while infrequently needed, do
occur in real applications and the work arounds needed when you've only got
a RAII idiom for locking/unlocking are
complex/dangerous/inefficient/hacks/all-of-the-above.

> Alas, I've seen time and again people doing the following terminal variant
> ...
> { // Some scope
> LockScope<Mutex>(mx);
> some_operation_guarded_by_mutex();
> }
> ...
> Is there anything in boost that prevents temporaries?

No, and I'm not sure of any techniques available that would prevent this
error. If you know of any I'd love to hear about it.

Bill Kempf


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk