Boost logo

Boost :

Subject: Re: [boost] Review Request: Singularity
From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2011-08-23 02:53:02


On Mon, Aug 22, 2011 at 5:15 PM, Mathias Gaunard
<mathias.gaunard_at_[hidden]> wrote:

> struct my_singleton
> {
> private:
>    my_singleton() {}
>    my_singleton(const my_singleton&);
>
> public:
>    static my_singleton& instance()
>    {
>        static my_singleton ins;
>        return ins;
>    }
> };
>
> which is simple, short, fast to compile, and thread-safe.
>

Thread-safe under which version of the language? Maybe C++11 (?), but
otherwise depends on the compiler and/or compiler flags, I think. In
particular, there is a race as to which thread is the "first" caller
to instace(), and thus a race on the construction of `ins`.

Tony


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