Boost logo

Boost :

Subject: Re: [boost] [thread] thread-safe static local
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2009-03-03 11:37:54


Oliver Kowalke wrote:
> I need a function 'get_default_X()' returning a reference to a singleton in a multithreaded env.
> I choosed Scott Meyers singleton pattern (static local) together with boost::call_once().
> My this be a reasonable, threadsafe implementation?

Hi Oliver,

I wasn't able to see your implementation because the mailing list
server still creates these broken links:

> -------------- next part --------------
> A non-text attachment was scrubbed...
> Name: test_static.cpp
> Type: text/x-c++src
> Size: 722 bytes
> Desc: not available
> URL: <http://lists.boost.org/MailArchives/boost/attachments/20090303/e842c976/attachment.bin>

It would be great if someone who knows how the mailing list works could
investigate this. In the meantime, perhaps you could put the files
online somewhere?

As you may know, gcc creates local statics that are thread safe by
default. So I have a macro something like this:

#if __GCC__
#define THREAD_SAFE_STATIC(TYPE,NAME) static TYPE NAME;
#else
#define THREAD_SAFE_STATIC(TYPE,NAME) #error FIXME
#endif

If your code can be used in the #else case, that would be great. I
think it's important to use the gcc feature when available as the
alternative will probably involve an unnecessary extra layer of locking.

Phil.


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