Boost logo

Boost :

Subject: Re: [boost] [thread] unlock on multiple lockables
From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2009-11-23 01:30:09


On Sun, Nov 22, 2009 at 9:08 PM, Vicente Botet Escriba
<vicente.botet_at_[hidden]> wrote:
>
> Hi,
>
> I don't know if I have already requested that but ...
>
> Is there any deep reason to don't provide the equivalent of the free
> functions lock, try_lock
>

My guesses at the reasons:

> Non-member function lock(Lockable1,Lockable2,...)
> Non-member function lock(begin,end)

Locking a bunch of locks is a recipe for deadlock. Do we lock them in
order? Then hold lock1, lock2, waiting to get lock3? Sounds
dangerous.
On the other hand, I suppose you could argue that using these
functions (exclusively) could guarantee that locks were always locked
in the same order (thus preventing deadlock)?

> Non-member function try_lock(Lockable1,Lockable2,...)
> Non-member function try_lock(begin,end)
>

Sounds even scarier - after trying lock1, do you try lock2? - so you
don't get lock1, you do get lock2, other thread gets the opposite ==
deadlock.

> to unlock multiple lockables?
>
> Non-member function unlock(Lockable1,Lockable2,...)
> Non-member function unlock(begin,end)
>
> Thanks,
> Vicente
> --

Only other reason would be because they are not common enough, and
easy enough to write on your own (using for_each, etc) for when you do
need them.

Oh, and lastly, because in general direct calls to lock/unlock are
discouraged (although at times useful/necessary) and scope_lock is
preferred.
So if you really wanted multiple locks, a multi_lock class should be
used. It's lock() function would lock a set of locks, etc. Then use
scope_lock.

Tony


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