静态变量和线程 [英] Static variables and threads

查看:83
本文介绍了静态变量和线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




A级

{

< ..>

};


const A& GetA()

{

静态A a;

返回a;

}

是多线程应用程序中的GetA()函数安全(一个和

只创建一个A)?我想线程不是c ++的一部分,但是

常见实现是什么呢? ?


谢谢,


Jeremie

Hi,

class A
{
<..>
};

const A& GetA()
{
static A a;
return a;
}
is the function GetA() safe in a multi-threaded application (one and
only one A is created) ? I suppose "threads" are not part of c++, but
what about the "common implementation" ?

Thanks,

Jeremie

推荐答案

" ;雷米" < JE ******* @ qqpart.com>写了...
"Jeremie" <je*******@qqpart.com> wrote...
A级
{
< ..>
};

const A& GetA()
{
静态A a;
返回一个;
}

是多线程应用程序中的GetA()函数安全(一个并且只创建一个A)?我想线程不是c ++的一部分,但是常见的实现是什么呢? ?
class A
{
<..>
};

const A& GetA()
{
static A a;
return a;
}
is the function GetA() safe in a multi-threaded application (one and
only one A is created) ? I suppose "threads" are not part of c++, but
what about the "common implementation" ?




请询问常见实施在comp.programming.threads。

C ++没有定义任何与线程相关的东西,因此有无法保证这两种方式。这可能就是为什么C ++编译器

实现者不关心线程问题。至少

如果我要实现一个C ++编译器,我就不会这样做。


据我了解线程,访问单例必须是制作

安全(使用可用的线程方式,如关键部分,互斥体,

等)否则当两个线程尝试时会遇到各种问题

同时更改对象或另外两个尝试使用对象'的
值,可以由其他两个线程中途更新...事实

''''被声明为const并不重要。您仍然可以在A中使用可变

数据,或者不受该类任何

对象的常量影响的静态成员。


当然,如果你能确保没有''GetA'的客户试图抛弃

远离它的常量并改变它_and_没有A对象有任何

可以改变的可变数据_和_类A没有任何静态数据

成员也可以在任何时候改变,然后是的,它是安全的,我猜猜。

但你真的可以确保满足所有条件吗?


Victor



Please ask about "common implementation" in comp.programming.threads.
C++ does not define anything related to threads and therefore there are
no guarantees one way or the other. That''s probably why C++ compiler
implementers do not concern themselves with threading issues. At least
I wouldn''t if I were to implement a C++ compiler.

As far as I understand threading, access to a singleton has to be made
safe (using available threading means, like critical sections, mutexes,
etc.) otherwise you run in all kinds of problems when two threads try
to simultaneously change the object or two others try to use the object''s
value that can be half-way updated by the other two threads... The fact
that ''A'' is declared const doesn''t matter. You can still have "mutable"
data in A, or static members that are not affected by constness of any
object of that class.

Of course, if you can make sure that no clients of ''GetA'' try to cast
away the constness of it and change it _and_ that no A object has any
mutable data that can change _and_ class A doesn''t have any static data
members that also can change at any point, then yes, it''s safe, I guess.
But can you really make sure that all those conditions are met?

Victor


Jeremie发布:
Jeremie posted:


A级
{
< ..>
};

const A& GetA()
{
静态A a;
返回一个;
}

是函数GetA()安全的多线程
应用程序(创建一个且只有一个A)?我想线程不是c ++的
部分,但是常见实现是什么呢? ?

谢谢,

Jeremie
Hi,

class A
{
<..>
};

const A& GetA()
{
static A a;
return a;
}
is the function GetA() safe in a multi-threaded application (one and only one A is created) ? I suppose "threads" are not part of c++, but what about the "common implementation" ?

Thanks,

Jeremie




非常简单的答案是查看你的代码和想想它

out。


如果第一次从Thread1调用GetA(),那么

grand。如果第一次从Thread2调用GetA(),

然后Thread2结束,那么Thread1是否能够继续工作

?答案是:

OFF-TOPIC

-JKop



The very simple answer is look at your code and figure it
out.

If GetA() is called for the first time from Thread1, then
grand. If GetA() is called for the first time from Thread2,
and then Thread2 ends, will Thread1 be able to still work
with it? The answer is:
OFF-TOPIC
-JKop


Jeremie写道:
Jeremie wrote:


A级
{
< ...>
};

const A& GetA()
{
静态A a;
返回一个;
}

是多线程应用程序中的GetA()函数安全(一个并且只创建一个A)?


我用过的所有编译器都错了,不做线程安全

初始化静态函数变量。


该标准确实保证在调用GetA时,变量'a''被初始化

。因此,C ++标准是知道。关于线程与否,实现必须保证这一点。


现在有一个gcc错误覆盖了这个。
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13684


有几次报道。


我想线程不是c ++的一部分,但是常见实现呢? ?

谢谢,

Jeremie
Hi,

class A
{
<..>
};

const A& GetA()
{
static A a;
return a;
}
is the function GetA() safe in a multi-threaded application (one and
only one A is created) ?
All compilers I have used get this wrong and do not do thread safe
initialization of static function variables.

The standard does guarentee that the variable ''a'' gets initialized
exactly once when GetA is called. Hence, wether the C++ standard
"knows" about threads or not, an implementation must guarentee this.

There is a gcc bug that covers this now.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13684

It has been reported a few times.

I suppose "threads" are not part of c++, but what about the "common implementation" ?

Thanks,

Jeremie



这篇关于静态变量和线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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