Meyers Item 4 Singleton in presence of threading

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In Scott Meyers' Effective C++ Item 4, Scott recommends using something like:

T& getaccess()
{
    static T localT;
    return localT;
}

so that a static local is used instead of a static global to access the global object and you can be sure it's initialized before used.

What happens in the presence of threading? What if two threads enter getaccess and exit in different orders? I'm ignoring here any threading issues that a T itself may have, and only thinking about the local static initialization itself.

Would the local t get initialized more than once?

The language says that t would only be initialized the first time the routine is called, so if the first one sleeps and a second one gets in and out before the first thread gets rescheduled, can it return a reference to localT before localT is initialized?

Patrick


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux