“Thread(oxabc)已经退出代码0x0”的多个副本“ [英] Multiple copies of "Thread (oxabc) has exited with code 0x0"

查看:85
本文介绍了“Thread(oxabc)已经退出代码0x0”的多个副本“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这实际上是一个多部分问题,其中心是我在程序中多次创建一个线程(不是同时),并且每次都很快就完成了。当我在运行一段时间后退出主程序时,我似乎得到了

线程已经退出代码(0x0)

每次启动线程的消息。



这可能看起来不是什么大不了的事,但我可能会在程序运行期间运行这段代码20-30K次,所以它显然可以节省很多某处的信息。我的意图是该程序将触发该线程,它将运行,退出,所有被释放然后它可以再次运行。



我也认为我可能正在接近问题不正确,我愿意接受建议。



我有一个机器人视觉(立体声)系统,它有一个旋转轴,电机在每个旋转角度触发一个摄像头触发器。两个摄像头正在等待触发,摄像头驱动程序在抓取图像时调用回调函数进行响应(两个摄像头同时抓取)。

摄像机的回调调用CreateThread,线程复制图像数据,进行一些处理并存储图像数据。我在一个帖子中这样做,因为有必要将程序松开以处理下一个相机抓取。



电话看起来像这样:

 {
...
HANDLE MyHandle;
DWORD MyThreadId;
MyHandle = CreateThread(NULL,NULL,ProcessImageThreadFunc,NULL,NULL,&(MyThreadID));
...
}



无符号 __stdcall ProcessImageThreadFunc( void  * pContext)
{
int cntr = 0 ;
int ndx = PathArray-> mImageRingNdx;

// 此部件等待,直到两个相机处于抓取状态 - 应该是darned
// 近同步 - 只有摄像头2调用CreateThread,因此摄像头1已经
// 捕获了一张图片,或者对此次电话会议很热。

while ((PathArray-> mImgGrabbed [ndx]!= 3 )& ;&(cntr < 2000000 ))
cntr ++;

GrabProc-> mFirstImgGrabbed = 0 ;

StatDlg-> UpdateStatusDisplay(1 | 16 | 32);

GrabProc-> CheckForLastImage( 0 ,ndx);

return 0 ;
}





一段时间后,当我完成所有我需要做的事情并且程序退出时,我得到了很多

线程(0x123)退出代码(0x0)

消息。 (0x123)部分对于每一行都是不同的。



我是不是正确地放弃了MyHandle或MyThreadID?



我不应该用线程做这个吗?



谢谢

解决方案

由于创建的开销,重复创建许多线程是个坏主意。重用线程要好得多。一种好的技术是每个同一个任务只使用一个线程,在循环中重复执行。要使线程等待新任务而不浪费任何CPU时间,请使用(如果是Windows)事件对象 http://msdn.microsoft.com/en-us/library/windows/desktop/ms682655%28v=vs.85% 29.aspx [ ^ ]。



限制线程:在通话中将其保持在等待状态通过保持此对象不发信号来 WaitForSingleObject 。当您有任务要提供给线程时,请发送信号。在调用wait方法时,线程将被关闭并且不会被调度回执行(这样,浪费没有CPU时间),直到它被来自不同线程的相同对象的信令唤醒。您可以将此机制与提供数据一起提供给线程,以便在一个名为阻塞队列的机制中工作。



-SA

This is really a multi-part question which centers around the fact that I create a thread multiple times in my program (not simultaneously) and it runs to completion very quickly each time. When I exit the main program quite after running for a while I seem to get the
Thread has exited with code (0x0)
message for each time the thread was started.

This may not seem a huge deal, but I potentially run this chunk of code 20-30K times during the run of the program, so it is obviously saving lots of information somewhere. My intention was that the program would fire the thread, it would run, exit, all be freed and then it could run again.

I also think I may be approaching the problem incorrectly and am open to suggestions.

I have a robotic vision (stereo) system which has a rotational axis and the motor fires a camera trigger every degree of rotation. Two cameras are waiting for the trigger and the camera driver responds by calling a Callback function when an image is grabbed (both cameras grab at the same time).
The callback for the cameras calls CreateThread and the thread copies the image data, does some processing and stores the image data. I do this in a thread because it is necessary to turn the program loose to handle the next camera grab.

The call looks something like this:

{
...
HANDLE MyHandle;
DWORD  MyThreadId;
MyHandle = CreateThread(NULL, NULL, ProcessImageThreadFunc, NULL, NULL, &(MyThreadID));
...
}


unsigned long __stdcall ProcessImageThreadFunc(void *pContext)
{
    int cntr = 0;
    int ndx  = PathArray->mImageRingNdx;

    // This part waits until both cameras are in a grabbed state - should be darned
    // near simultaneous - only camera 2 calls CreateThread, so camera 1 has already
    // captured an image or is hot on the heals of this call.

    while( ( PathArray->mImgGrabbed[ndx] != 3 ) && ( cntr < 2000000 ) )
        cntr++;

    GrabProc->mFirstImgGrabbed = 0;

     StatDlg->UpdateStatusDisplay(1|16|32);

    GrabProc->CheckForLastImage( 0, ndx );

    return 0;
}



Some time later, when I have done all I need to do and the program exits, I get LOTS of
Thread (0x123) has exited with code (0x0)
messages. The (0x123) part is different for each line.

Am I not letting go of the MyHandle or MyThreadID properly or something?

Should I not be doing this with a thread?

Thanks

解决方案

It's a bad idea to repeatedly create many threads due to the overhead of their creation. It's much better to reuse the threads. One of the good techniques is to use just one thread per task of the same kind repeating its execution in a loop. To make the thread waiting for the new task and not wasting any CPU time, use (in case of Windows) an event object: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682655%28v=vs.85%29.aspx[^].

Throttle a thread: keep it in the wait state at the call to WaitForSingleObject by keeping this object non-signalled. When you have a task to feed to the thread, signal the object. At the call of wait methods, a thread will be switched off and not scheduled back to execution (this way, wasting no CPU time) until it is waken up by the signalling of the same object from a different thread. You can wrap this mechanism together with supplying the data to feed to the thread to work at in a single mechanism called blocking queue.

—SA


这篇关于“Thread(oxabc)已经退出代码0x0”的多个副本“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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