Boost logo

Boost :

From: Martin Wille (mw8329_at_[hidden])
Date: 2005-07-24 10:06:45


John Maddock wrote:

> That call_once idea is really cunning. Embarrissingly I only invented
> static_mutex because I couldn't think of a way of getting call_once to do
> what I wanted!

I think your static_mutex would perform better than call_once based code.

[...]
> All untried:
>
> template <class Mut, class Tag>
> Mut& do_get_mutex()
> {
> static Mut m;
> return m;
> }
>
> template <class Mut, class Tag>
> void call_once_proc()
> {
> do_get_mutex();
> }

It's this function I wanted to get rid of when I proposed an overload
for call_once in the postscript of my message.

> template <class Mutex, class id>
> Mutex& get_static_mutex()
> {
> boost::once_flag once = BOOST_ONCE_INIT;

This needs 'static'.

> boost::call_once(&call_once_proc<Mutex, id>, once);
> return do_get_mutex<Mutex, id>();
> }

> How does this look as an extension to Boost.Threads?

This is very helpful and it likely would get used often. Its only
drawback is the need for the unique tag type (well, I guess someone will
suggest some macro for that).

                                                        Unless I've missed
> something (!) it should work for any mutex type. Actually shouldn't this
> work for any global variable that you want initialised in a thread safe
> manner?

Hmm, shouldn't a general solution become part of a singleton library?

Boost.Thread should offer a threadsafe (to initialize) mutex if it can
do better than a generic version. This seems to be the case here.

Regards,
m
Send instant messages to your online friends http://au.messenger.yahoo.com


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