线程安全 [英] Thread safety

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

问题描述

以下线程是否安全:


1.假设对象是任何简单对象

对象* fn()

{

对象* p =新对象();

返回p;

}


2.按值返回线程安全吗?


3. int * p = new int

这个线程安全吗?


谢谢

解决方案

2007-06-06 15:29, ar ******** @ gmail.com 写道:


以下是线程安全:



这不是这个组的问题,因为C ++没有定义

线程,你应该在一个问题中提出这样的问题您的平台组/

线程库。


1.假设对象是任何简单对象

对象* fn()

{

对象* p =新对象();

返回p;

}


如果分配器是线程安全的那么是。


2.是按值线程安全返回的吗?



堆栈上的所有内容本质上都应该是线程安全的。


3. int * p = new int

这个线程安全吗?



见1.


-

Erik Wikstr?m


6月6日上午8:29,arun.da ... @ gmail.com写道:


以下是线程安全:

1.假设对象是任何简单对象

对象* fn()

{

对象* p = new Object();

返回p;


}



关于此(1)我没有看到这种伤害,即使我也不知道这样做,


>

2.按价值线程返回安全?


3. int * p = new int



这个,我认为它没有意义。


这个线程安全吗?

谢谢



6月6日晚上8:29,arun.da ... @ gmail.com写道:


以下线程是否安全:


1.假设对象是任何简单对象

对象* fn()

{

对象* p =新对象();

返回p;


}


2.返回者值线程安全吗?


3. int * p = new int

这个线程安全吗?


谢谢



每个线程都有自己的堆栈和寄存器帧。因此,所有

都不能访问全局分配的对象是安全的。在你的代码中,就像

一样,只有第三种情况看起来像是在全球空间中(再次,

它在你的帖子中)。


Are the following thread safe:

1. Assuming Object is any simple object
Object* fn()
{
Object *p = new Object();
return p;
}

2. is return by value thread safe?

3. int *p = new int
is this thread safe?

Thanks

解决方案

On 2007-06-06 15:29, ar********@gmail.com wrote:

Are the following thread safe:

This is not a question for this group, since C++ does not define
threads, you should ask such questions in a group for your platform/
threading library.

1. Assuming Object is any simple object
Object* fn()
{
Object *p = new Object();
return p;
}

If the allocator is thread-safe then yes.

2. is return by value thread safe?

Everything on the stack should be thread-safe by nature.

3. int *p = new int
is this thread safe?

See 1.

--
Erik Wikstr?m


On Jun 6, 8:29 am, arun.da...@gmail.com wrote:

Are the following thread safe:
1. Assuming Object is any simple object
Object* fn()
{
Object *p = new Object();
return p;

}

about this(1)i dont see the harm, even doe i woldnt do it so,

>
2. is return by value thread safe?

3. int *p = new int

and this, i thinck it''s meaningless.

is this thread safe?

Thanks



On Jun 6, 8:29 pm, arun.da...@gmail.com wrote:

Are the following thread safe:

1. Assuming Object is any simple object
Object* fn()
{
Object *p = new Object();
return p;

}

2. is return by value thread safe?

3. int *p = new int
is this thread safe?

Thanks

Each thread has it`s own stack and register frame. So everything that
does not access globally allocated objects is safe. In your code, as
it is, only the third case looks like its in a global space (again, as
it is in your post).


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

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