Click here to Skip to main content
14,932,407 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
{
...
HANDLE MyHandle;
DWORD  MyThreadId;
MyHandle = CreateThread(NULL, NULL, ProcessImageThreadFunc, NULL, NULL, &(MyThreadID));
...
}

C#
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
Posted
Comments
   
At least tag your language and platform, use Improve question.
—SA

1 solution

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
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900