编写单例类 [英] Writing Singleton Classes

查看:72
本文介绍了编写单例类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在本教程中使用C ++中的单例类

http://gethelp.devx.com/techtips/cpp.../10min0200.asp

author给出了一个简单的单例类的两个实现,声称

只有第一个对多线程应用是安全的。我想

知道为什么会这样。


课程内容如下:


class Singleton

{

public:

static Singleton * Instance();

protected:

Singleton();

Singleton(const Singleton&);

Singleton& operator =(const Singleton&);

private:

静态Singleton * pinstance;

};


第一个实现是:


Singleton * Singleton :: pinstance = 0; //初始化指针

Singleton * Singleton :: Instance()

{

if(pinstance == 0)//这是第一次通话吗?

{

pinstance =新单身人士; //创建唯一的实例

}

返回pinstance; //唯一实例的地址

}

Singleton :: Singleton()

{

// ..执行必要的实例初始化

}


第二个实现对构造函数进行了一些小改动:


Singleton * Singleton :: Instance()

{

静态Singleton inst;

return& inst;

}


作者指出,第二个实现针对

单线程应用程序进行了优化。为什么它不适用于

多线程应用程序?


谢谢,

cpp

解决方案

cppaddict写道:



在本教程中关于C ++中的单例类( http://gethelp.devx.com/techtips/ cpp ... / 10min0200.asp
作者给出了一个简单的单例类的两个实现,声称只有第一个对多线程应用是安全的。我想
知道为什么会这样。
...




我在这里找到答案:
http://blogs.msdn.com/oldnewthing/ar .. ./08/85901.aspx


现在,第一个实现也存在问题,并且确实存在同样的问题:初始化必须是保护

用互斥量等等......


Benoit


cppaddict写道:



在本教程中关于C ++中的单例类
知道为什么会这样。

课程如下:

班Singleton
{
公开: static Singleton * Instance();
protected:
Singleton();
Singleton(const Singleton&);
Singleton& operator =(const Singleton&);
private:
static Singleton * pinstance;
};


在我看来,* first * instance不是线程安全的,但第二个是!

第一个实现是:

Singleton * Singleton :: pinstance = 0; //初始化指针
Singleton * Singleton :: Instance()
{
if(pinstance == 0)//是它第一次打电话?
{
如果在这里发生上下文切换会怎样?在if b / b
之后的状态,但在新的之前? pinstance = new Singleton; //创建唯一的实例
}
返回pinstance; //唯一实例的地址
}
Singleton :: Singleton()
// ...执行必要的实例初始化
}
不是构造函数,而是Singleton访问器。
据我所知,这* *是线程安全的。 Singleton * Singleton :: Instance()
{
静态Singleton inst;
返回& inst;
}
作者注意到第二个实现针对单线程应用程序进行了优化。为什么它不适用于
多线程应用程序?




我认为作者充满了它,但那只是* MY *意见......我

可能是错的。


现在,第一个实现是也有问题,并且具有相同的问题:初始化必须通过互斥锁或其他东西来保护......

Benoit



感谢您的参考。这很有趣......


但是什么是互斥?看完那篇文章后,似乎

单身人士绝不是线程安全的。


谢谢,

cpp


Hi,

In this tutorial on singleton class in C++
(
http://gethelp.devx.com/techtips/cpp.../10min0200.asp) the
author gives two implementations of a simple singleton class, claiming
that only the first is safe for multi-threaded appliactions. I want
to know why this so.

The class is as follows:

class Singleton
{
public:
static Singleton* Instance();
protected:
Singleton();
Singleton(const Singleton&);
Singleton& operator= (const Singleton&);
private:
static Singleton* pinstance;
};

The first implementation is:

Singleton* Singleton::pinstance = 0;// initialize pointer
Singleton* Singleton::Instance ()
{
if (pinstance == 0) // is it the first call?
{
pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}
Singleton::Singleton()
{
//... perform necessary instance initializations
}

The second implementation makes a small change to the constructor:

Singleton* Singleton::Instance ()
{
static Singleton inst;
return &inst;
}

The author notes that the second implementation is optimized for
single-threaded applications. Why would it not also work for
multi-threaded applications?

Thanks,
cpp

解决方案

cppaddict wrote:

Hi,

In this tutorial on singleton class in C++
(http://gethelp.devx.com/techtips/cpp.../10min0200.asp) the
author gives two implementations of a simple singleton class, claiming
that only the first is safe for multi-threaded appliactions. I want
to know why this so.
...



I found an answer here :
http://blogs.msdn.com/oldnewthing/ar.../08/85901.aspx

Now, the first implementation is also problematic, and has
exactely the same problem: initialisation must be protected
by a mutex or something...

Benoit


cppaddict wrote:

Hi,

In this tutorial on singleton class in C++
(http://gethelp.devx.com/techtips/cpp.../10min0200.asp) the
author gives two implementations of a simple singleton class, claiming
that only the first is safe for multi-threaded appliactions. I want
to know why this so.

The class is as follows:

class Singleton
{
public:
static Singleton* Instance();
protected:
Singleton();
Singleton(const Singleton&);
Singleton& operator= (const Singleton&);
private:
static Singleton* pinstance;
};
Looks to me like the *first* instance isn''t thread safe, but the second is!
The first implementation is:

Singleton* Singleton::pinstance = 0;// initialize pointer
Singleton* Singleton::Instance ()
{
if (pinstance == 0) // is it the first call?
{ What happens if a context switch occurs right here? After the if
statemnt, but before the new? pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}
Singleton::Singleton()
{
//... perform necessary instance initializations
}

The second implementation makes a small change to the constructor: Not a constructor, but the Singleton accessor. As far as I can tell, this *is* thread safe. Singleton* Singleton::Instance ()
{
static Singleton inst;
return &inst;
}

The author notes that the second implementation is optimized for
single-threaded applications. Why would it not also work for
multi-threaded applications?



I think the author is full of it, but that''s just *MY* opinion... I
could be wrong.


Now, the first implementation is also problematic, and has
exactely the same problem: initialisation must be protected
by a mutex or something...

Benoit



Thanks for the reference. That''s very interesting....

What is a mutex though? After reading that article it seems that
Singletons are never thread safe.

Thanks,
cpp


这篇关于编写单例类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆