Boost logo

Boost :

From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2006-11-03 03:34:10

Anthony Williams wrote:
> "Once" is more low-level. It can be used to initialize a mutex, *or any other
> type of object*. Yes, if you have a static mutex, you can lock the mutex
> around the initialization of another object, but that's unnecessarily complex:

Sorry, but I disagree. My reasoning is that only one concept is better
than two for the same purpose, i.e. lock, instead of two i.e. lock and once.

Once you understand locking, you necessarily understand protection
by locks. On the other hand you will need to look up what once
exactly is about the first time you encounter it. To me at least
its purpose was not obvious on first encounter.

> You can't use a scoped_lock for this unless it has an unlock() member (which
> IMO defeats the point of it being scoped), since otherwise you end up
> serializing the "access x" part too.

I don't think so:

void f()
     static mutex m;
     scoped_lock lk(m);
     static some_class x;
     // access x

You cannot defeat the purpose of scoped_lock! In this case it is
conditionally unlocked when the scope is left. This in the first
place is, why Peter Dimov came up with an example that proved me
that you need be able to do locking directly on the mutex too.

This also is why the scoped_lock isn't thread safe. In the destructor
it has to check an unprotected variable that tests the current
lock status.


Boost list run by bdawes at, gregod at, cpdaniel at, john at