循环分配数组对象 [英] allocation of an array object in loop

查看:89
本文介绍了循环分配数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.
我还想添加其他信息.
对于上述程序.当我使用follow时,如何分配该数组.
首先它初始化一个:

hallo.
i also wanted to add additional information.
for the above mentioned programm. how could i dellocated this array when i use follow.
first it initialize a:

for (int i = 0; i < 100; i++){
joblist[i] = new JobOptions(some parameter);
}
while (true){
for (int i = 0; i < 100; i++){
Sleep(200);
if ((run == true) && testnumber< number) {
AfxBeginThread(VTKAppThread,(LPVOID)joblist[i]);
}
}
}



我的问题是:我该如何在while循环内释放对象"joblist"?


很抱歉收到大量评论.我的计算机发疯了



my question is: How can i delocate the object "joblist" inside the while-loop?


Sorry for the high number of reviews. My computer got crazy

推荐答案

为什么要在那里分配它?这整个事情对我来说似乎有些动摇.执行从哪里离开while循环? testnumbernumber在哪里变化?为什么对100进行硬编码?

由于您要将joblist中的指针传递给线程函数,因此在确定工作线程已完成或不再使用它们之前,您不能安全地删除它们.有很多方法可以做到这一点,一种方法是跟踪AfxBeginThread的返回值,并使用WaitForMultipleObjects等待所有线程句柄发出信号.另一个方法是使用一些线程同步来分别保护joblist中的指针,然后等待工作线程将其标记为未使用".
Why would you want to deallocate it there? This whole thing looks kinda shaky to me. Where does execution ever leave the while loop? Where do testnumber and number ever change? Why are the 100s hard coded?

Since you''re passing the pointers in joblist to the thread function you can''t safely delete them until you''re sure that the worker threads are finished or are no longer using them. There are plenty of ways to do this, one would be to keep track of the returns from AfxBeginThread and use WaitForMultipleObjects to wait for all of the thread handles to become signaled. Another would be to use some thread synchronization to protect the pointers in joblist individually and then wait for the worker threads to mark them as "unused".


因为您正在启动线程,你不能.这些线程不会在完成工作时通知您,因此您无法释放可能仍在使用的内容.

我同意另一个人的观点,整个事情对我来说真的很糟糕.
Because you''re starting threads, you can''t. Those threads are not letting you know when they finish their work, so you can''t deallocate something that could still be getting used.

I agree with the other guy, the whole thing looks really bad to me.


您可以通过AfxBeginThread(pfThread, new Job(..))
启动线程
并让它(线程)在pfThread的出口处delete其参数:)
You could start a thread by AfxBeginThread(pfThread, new Job(..))

and let it (the thread) delete its parameter at exit from pfThread :)


这篇关于循环分配数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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