Edit: /usr/include/stxxl/bits/singleton.h (2236B)
/***************************************************************************
* include/stxxl/bits/singleton.h
*
* Part of the STXXL. See http://stxxl.sourceforge.net
*
* Copyright (C) 2008, 2011 Andreas Beckmann
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
**************************************************************************/
#ifndef STXXL_SINGLETON_HEADER
#define STXXL_SINGLETON_HEADER
#include
#include
#include
#include
#include
STXXL_BEGIN_NAMESPACE
template
class singleton : private noncopyable
{
typedef INSTANCE instance_type;
typedef instance_type* instance_pointer;
typedef volatile instance_pointer volatile_instance_pointer;
static volatile_instance_pointer instance;
static instance_pointer create_instance();
static void destroy_instance();
public:
inline static instance_pointer get_instance()
{
if (!instance)
return create_instance();
return instance;
}
};
template
typename singleton::instance_pointer
singleton::create_instance()
{
static mutex create_mutex;
scoped_mutex_lock instance_write_lock(create_mutex);
if (!instance) {
instance = new instance_type();
if (destroy_on_exit)
register_exit_handler(destroy_instance);
}
return instance;
}
template
void singleton::destroy_instance()
{
instance_pointer inst = instance;
//instance = NULL;
instance = reinterpret_cast(unsigned_type(-1)); // bomb if used again
delete inst;
}
template
typename singleton::volatile_instance_pointer
singleton::instance = NULL;
STXXL_END_NAMESPACE
#endif // !STXXL_SINGLETON_HEADER