Posted on 2009-09-22 00:38
Fox 閱讀(7439)
評論(9) 編輯 收藏 引用 所屬分類:
T技術碎語
本文同步自游戲人生
以前曾經討論過Singleton的實現,這次在對照ACE和Boost代碼的時候,又重新審視了一下二者對Singleton不同的實現。其間的差別也體現了不同的編程哲學:ACE的實現更加偏重多線程中的安全和效率問題;Boost的實現則偏重于使用語言自身的特性滿足Singleton模式的基本需求。
o ACE的實現
Douglas C. Schmidt在Double-Checked Locking: An Optimization Pattern for Efficiently Initializing and Accessing Thread-safe Objects一文中對double-check lock(一般譯為雙檢鎖)進行了詳細的闡述。
ACE的Singleton使用Adapter模式實現對其他類的適配,使之具有全局唯一的實例。由于C++標準并非明確指定全局靜態對象的初始化順序,ACE使用double-check lock保證線程安全,并使之不受全局靜態對象初始化順序的影響,同時也避免了全局靜態實現方式的初始化后不使用的開銷。
如果你能夠準確的區分以下三種實現的弊端和隱患,對double-check lock也就有了足夠的了解。
// -------------------------------------------
class Singleton
{
public:
static Singleton *instance (void)
{
// Constructor of guard acquires
// lock_ automatically.
Guard<Mutex> guard (lock_);
// Only one thread in the
// critical section at a time.
if (instance_ == 0)
instance_ = new Singleton;
return instance_;
// Destructor of guard releases
// lock_ automatically.
}
private:
static Mutex lock_;
static Singleton *instance_;
};
// ---------------------------------------------
static Singleton *instance (void)
{
if (instance_ == 0) {
Guard<Mutex> guard (lock_);
// Only come here if instance_
// hasn’t been initialized yet.
instance_ = new Singleton;
}
return instance_;
}
// ---------------------------------------------
class Singleton
{
public:
static Singleton *instance (void)
{
// First check
if (instance_ == 0)
{
// Ensure serialization (guard
// constructor acquires lock_).
Guard<Mutex> guard (lock_);
// Double check.
if (instance_ == 0)
instance_ = new Singleton;
}
return instance_;
// guard destructor releases lock_.
}
private:
static Mutex lock_;
static Singleton *instance_;
};
更多詳情,見Schmidt老師的原文和ACE_Singleton實現。
o Boost的實現
Boost的Singleton也是線程安全的,而且沒有使用鎖機制。當然,Boost的Singleton有以下限制(遵從這些限制,可以提高效率):
o The classes below support usage of singletons, including use in program startup/shutdown code, AS LONG AS there is only one thread running before main() begins, and only one thread running after main() exits.
o This class is also limited in that it can only provide singleton usage for classes with default constructors.
// T must be: no-throw default constructible and no-throw destructible
template <typename T>
struct singleton_default
{
private:
struct object_creator
{
// This constructor does nothing more than ensure that instance()
// is called before main() begins, thus creating the static
// T object before multithreading race issues can come up.
object_creator() { singleton_default<T>::instance(); }
inline void do_nothing() const { }
};
static object_creator create_object;
singleton_default();
public:
typedef T object_type;
// If, at any point (in user code), singleton_default<T>::instance()
// is called, then the following function is instantiated.
static object_type & instance()
{
// This is the object that we return a reference to.
// It is guaranteed to be created before main() begins because of
// the next line.
static object_type obj;
// The following line does nothing else than force the instantiation
// of singleton_default<T>::create_object, whose constructor is
// called before main() begins.
create_object.do_nothing();
return obj;
}
};
template <typename T>
typename singleton_default<T>::object_creator
singleton_default<T>::create_object;
對于多數Singleton使用,Boost提供的版本完全能夠滿足需求。為了效率,我們有必要對其使用作出一定的限制。
而在多線程編程中,則有必要使用double-check lock降低頻繁加鎖帶來的開銷。
-------------------------------------------------------------------------------
PS: 欣賞Soft的一句話:經得起誘惑,耐得住寂寞。