Boost logo

Threads-Devel :

From: David Abrahams (dave_at_[hidden])
Date: 2006-03-23 16:44:21

Anthony Williams <anthony_at_[hidden]> writes:

>>> The need for static mutexes might be reduced in C++, but there are
>>> still use cases,
>> Yes. The question is whether they are general enough to warrant
>> supporting them. I contend that they are not, at least not in the
>> next few revisions of the library.
> You're probably right. I started down this line of thought when
> rewriting boost::call_once, and noticed that mutexes at global scope
> are used everywhere, and yet they are not thread-safe for
> initialization. Maybe I could do with some perspective.

I may have led you astray.

p. 132 of Butenhof says "The main reason for pthread_once is that you
were not originally allowed to statically initialize a mutx. Thus, to
use a mutex, you had to first call pthread_mutex_init. You must
initialize a mutex only once, so the initialization call must be made
in one-time initialization code. The pthread_once function solved
this recursive problem. When static initialization of mutexes was
added to the standard, pthread_once was retained as a convenience
function. If it's convenient, use it, but remember that you don't
have to use it."

So it seems that a statically initializable mutex is more efficient
than pthread_once on at least some platforms where a statically
initializable mutex is possible. If such a mutex exists everywhere,
we should support it. If not, it seems like a nicety but not much
help for portable code.

Dave Abrahams
Boost Consulting

Threads-Devel list run by bdawes at, david.abrahams at, gregod at, cpdaniel at, john at