new和从不同的线程中删除 [英] new and delete from different threads

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

问题描述

我知道目前形式的C ++标准并没有说明关于线程的任何内容,但是我确实有一个相关的问题。


我我正在使用Windows XP / VC ++ 8.0。


是否有问题来自一个线程的一堆对象和

在另一个线程中删除它们?我做的事情如下:


struct GenericPointerDeleter

{

模板< typename T>

无效operator()(T * p)

{

if(ptr!= 0)

{

delete ptr;

ptr = 0;

}

}

};


typedef vector< classofpointers *> vecptrs;

int main()

{

vecptrs myptrs;

CreateThread(...... (LPVOID)& myptrs ........);

//使用一些特定于平台的API等待

threadcallbackroutine终止

pseudo_wait_for_terminate(threadcallbackroutine);


for_each(myptrs.begin(),myptrs.end(),GenericPointerDeleter());

}


DWORD WINAPI threadcallbackroutine(LPVOID param)

{

vecptrs * my_ptrs = static_cast< vecptrs *>(param);

my_ptrs-> push_back(new classofpointers());

返回0;

}


我大大简化了我的

应用程序中实际发生的事情...


我应该小心新的'并且从同一个线程中删除吗?

解决方案



Dilip写道:

我知道C ++标准目前的形式没有说关于线程的任何内容,但是我确实有一个相关的问题。


是什么让你对主题提出疑问?除了关于线程的问题之外我什么都没看到,这个问题不是主题,因为这个新闻组

只涵盖标准C ++。

我正在使用Windows XP / VC ++ 8.0。


是的,但这与这个新闻组无关。

我应该小心新的'并且从同一个帖子中删除吗?




如果你在同一个线程中执行new和delete(在一个多线程的
应用程序中),那么与new和删除

单线程应用程序?


Dilip写道:

.... < blockquote class =post_quotes>是否有问题从一个线程中新建一堆对象并在另一个线程中删除它们? ...




在Win32,Linux,MAC或其他许多方面做这件事应该没问题

我已经参与过的工作。


对于某些版本的malloc来说,它的效率会稍微低一些,但除非性能是一个大问题,否则你应该没有后顾之忧。


在文章< 11 ********************* @ u72g2000cwu.googlegroups中。 com>,

" Dilip" < RD ***** @ lycos.com>写道:

我知道目前形式的C ++标准并没有说明关于线程的任何内容,但我确实有一个相关的问题。

是否有问题从一个线程中删除一堆对象并在另一个线程中删除它们?我做了类似的事情:

struct GenericPointerDeleter
{
模板< typename T>
void operator()(T * p)
{
if(ptr!= 0)
{
删除ptr;
ptr = 0;
}
}
};

typedef vector< classofpointers *> vecptrs;
int main()
{vecptrs myptrs;
CreateThread(......(LPVOID)& myptrs ........);
//使用一些特定于平台的API来等待
threadcallbackroutine终止
pseudo_wait_for_terminate(threadcallbackroutine);

for_each(myptrs.begin(),myptrs.end() ,GenericPointerDeleter());


DWORD WINAPI threadcallbackroutine(LPVOID参数)
{
vecptrs * my_ptrs = static_cast< vecptrs *>(param);
my_ptrs-> push_back(new classofpointers());
返回0;
}

我大大简化了我的
应用程序中实际发生的事情...

我应该注意新的并且从同一个帖子中删除吗?




我没有访问权限到Windows,但我相信你的代码很好。

C ++委员会对C ++ 0X感兴趣,有一个多线程内存模型

,这可能是基于当前的做法。至于我知道,目前的做法是将内存所有权转移到

线程边界是可以的。


-Howard


I am aware that the C++ standard in its present form does not say
anything about threads, however I do have a relevant question.

I am working on Windows XP/VC++ 8.0.

Is there a problem new''ing a bunch of objects from one thread and
deleting them in another? I do something like:

struct GenericPointerDeleter
{
template<typename T>
void operator()(T* p)
{
if (ptr != 0)
{
delete ptr;
ptr = 0;
}
}
};

typedef vector<classofpointers*> vecptrs;
int main()
{
vecptrs myptrs;
CreateThread(...... (LPVOID)&myptrs........);
// use some platform specific API to wait until
threadcallbackroutine terminates
pseudo_wait_for_terminate(threadcallbackroutine);

for_each(myptrs.begin(), myptrs.end(), GenericPointerDeleter());
}

DWORD WINAPI threadcallbackroutine(LPVOID param)
{
vecptrs* my_ptrs = static_cast<vecptrs*>(param);
my_ptrs->push_back(new classofpointers());
return 0;
}

I have vastly simplified what is essentially happening in my
application...

Should I be careful about new''ing and deleting from the same thread?

解决方案


Dilip wrote:

I am aware that the C++ standard in its present form does not say
anything about threads, however I do have a relevant question.

What makes your question on topic? I don''t see anything other than a
question about threading, which is not on topic since this newsgroup
only covers standard C++.
I am working on Windows XP/VC++ 8.0.
Yes, but that''s irrelevant on this newsgroup.
Should I be careful about new''ing and deleting from the same thread?



If you do new and delete in the same thread ( in a multi-threaded
application ), how is that any different than doing new and delete in a
single threaded application?


Dilip wrote:
....

Is there a problem new''ing a bunch of objects from one thread and
deleting them in another? ...



There should be no problem doing that on Win32, Linux, MAC or many other
unicies I''ve worked on.

For some versions of malloc, it will be somewhat less efficient but
unless performance is a big issue, then you should have no worries.


In article <11*********************@u72g2000cwu.googlegroups. com>,
"Dilip" <rd*****@lycos.com> wrote:

I am aware that the C++ standard in its present form does not say
anything about threads, however I do have a relevant question.

I am working on Windows XP/VC++ 8.0.

Is there a problem new''ing a bunch of objects from one thread and
deleting them in another? I do something like:

struct GenericPointerDeleter
{
template<typename T>
void operator()(T* p)
{
if (ptr != 0)
{
delete ptr;
ptr = 0;
}
}
};

typedef vector<classofpointers*> vecptrs;
int main()
{
vecptrs myptrs;
CreateThread(...... (LPVOID)&myptrs........);
// use some platform specific API to wait until
threadcallbackroutine terminates
pseudo_wait_for_terminate(threadcallbackroutine);

for_each(myptrs.begin(), myptrs.end(), GenericPointerDeleter());
}

DWORD WINAPI threadcallbackroutine(LPVOID param)
{
vecptrs* my_ptrs = static_cast<vecptrs*>(param);
my_ptrs->push_back(new classofpointers());
return 0;
}

I have vastly simplified what is essentially happening in my
application...

Should I be careful about new''ing and deleting from the same thread?



I don''t have access to Windows, but I believe your code is fine. The
C++ committee is interested in C++0X have a multi-threaded memory model
which will probably be based on current practice today. And as far as I
know, current practice is that transferring memory ownership across
thread boundaries is fine.

-Howard


这篇关于new和从不同的线程中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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