Boost logo

Threads-Devel :

From: David Abrahams (dave_at_[hidden])
Date: 2006-03-06 13:36:33


Roland Schwarz <roland.schwarz_at_[hidden]> writes:

> David Abrahams wrote:
>> [...] Mutex
>> objects at global scope generally protect data at global scope.
>> Unless that data has a trivial ctor, its initialization needs to be
>> protected by a once routine anyway.
>
> Most likely I am missing something here, but why would I need a once
> in a global object in the general case? Isn't this just for
> singletons or create on first use?

Generally. And that's *usually* the only reason you'd want a
statically initialized mutex. Anything else can effectively rely on
dynamic initialization.

> As I see it, guarding creation of a mutex at global scope is
> pointless as long as I do not guard the ctor of the global object it
> is refering to at the same time.

That's exactly my point. In general, in the cases where you'd be
tempted to use a statically initialized mutex, you need to guard
something with a once routine anyway. May as well just guard the
mutex, too, in that case.

> This is also one of the points I tried to express in one of my
> previous posts already.
>
> The protected data and its mutex should be tightly (explicitely)
> coupled. Of course a once like mechanism will be used under the hood
> to prevent the race condition during creation, but this need not be
> exposed to the user.

That was the singleton template I showed in my post.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk