Posted on 2009-09-22 00:38
Fox 閱讀(7439)
評(píng)論(9) 編輯 收藏 引用 所屬分類(lèi):
T技術(shù)碎語(yǔ)
本文同步自游戲人生
以前曾經(jīng)討論過(guò)Singleton的實(shí)現(xiàn),這次在對(duì)照ACE和Boost代碼的時(shí)候,又重新審視了一下二者對(duì)Singleton不同的實(shí)現(xiàn)。其間的差別也體現(xiàn)了不同的編程哲學(xué):ACE的實(shí)現(xiàn)更加偏重多線程中的安全和效率問(wèn)題;Boost的實(shí)現(xiàn)則偏重于使用語(yǔ)言自身的特性滿足Singleton模式的基本需求。
o ACE的實(shí)現(xiàn)
Douglas C. Schmidt在Double-Checked Locking: An Optimization Pattern for Efficiently Initializing and Accessing Thread-safe Objects一文中對(duì)double-check lock(一般譯為雙檢鎖)進(jìn)行了詳細(xì)的闡述。
ACE的Singleton使用Adapter模式實(shí)現(xiàn)對(duì)其他類(lèi)的適配,使之具有全局唯一的實(shí)例。由于C++標(biāo)準(zhǔn)并非明確指定全局靜態(tài)對(duì)象的初始化順序,ACE使用double-check lock保證線程安全,并使之不受全局靜態(tài)對(duì)象初始化順序的影響,同時(shí)也避免了全局靜態(tài)實(shí)現(xiàn)方式的初始化后不使用的開(kāi)銷(xiāo)。
如果你能夠準(zhǔn)確的區(qū)分以下三種實(shí)現(xiàn)的弊端和隱患,對(duì)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_;
};
更多詳情,見(jiàn)Schmidt老師的原文和ACE_Singleton實(shí)現(xiàn)。
o Boost的實(shí)現(xiàn)
Boost的Singleton也是線程安全的,而且沒(méi)有使用鎖機(jī)制。當(dāng)然,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;
對(duì)于多數(shù)Singleton使用,Boost提供的版本完全能夠滿足需求。為了效率,我們有必要對(duì)其使用作出一定的限制。
而在多線程編程中,則有必要使用double-check lock降低頻繁加鎖帶來(lái)的開(kāi)銷(xiāo)。
-------------------------------------------------------------------------------
PS: 欣賞Soft的一句話:經(jīng)得起誘惑,耐得住寂寞。