Link to home
Start Free TrialLog in
Avatar of turbot_yu
turbot_yuFlag for Singapore

asked on

How to close thread

Hi Experts,

Is there standard way to close the thread?
May you offer the sample code?

And in my case, most varibles in each thread is just for the thread itself.
Some are global ones. Some may talk to other thread.
How to define them?
How to implement it?

Thanks and Regards,

Turbot
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of turbot_yu

ASKER

Hi AlexFM May I have the detail code for WaitForSingleObject(hThread, INFINITE)

Thanks,

Turbot
Avatar of AlexFM
AlexFM

What details?
I have create a thread by
      hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)PQ_Son,NULL,0,&iID);
long WINAPI PQ_Son(long Para)
{
}

May I know how to close it?
Start thread:

HANDLE hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)PQ_Son,NULL, (LPVOID)hStopEvent ,&iID);

Stop thread:

// Ask thread to stop
SetEvent(hStopEvent);
// Wait when thread really exits
WaitForSingleObject(hThread, INFINITE);

Thread function:

long WINAPI PQ_Son(long Para)
{
    HANDLE hStop = (HANDLE)Para;

    while (...)
    {
        // do something ...

        // test Stop event
        if ( WaitForSingleObject(hStop, 0) == WAIT_OBJECT_0 )
        {
            break;
        }
    }

    return 0;
}
The '// Wait when thread really exits' is common for all the thread usage, right?

That is all the thread need to wait to close? After it really finish.


If I am not wrong, I can use either CreateEvent or CreateThread.
And I can choose to use 'ask to stop' or 'wait to stop'.
Am I right?

Thanks,

Turbot
If I am not wrong, I can use either CreateEvent or CreateThread.
And I can choose to use 'ask to stop' or 'wait to stop'.
Am I right?

Thanks,

Turbot
In the below lines, will do something run more than one time?
If I want the thread finish it by it self, how to set the para of the line of  if ( WaitForSingleObject
   while (...)
    {
        // do something ...

        // test Stop event
        if ( WaitForSingleObject(hStop, 0) == WAIT_OBJECT_0 )
        {
            break;
        }
    }
>> I can use either CreateEvent or CreateThread

CreateThread creates the thread - this is your purpose. CreateEvent creates helper object used for cross-thread communications.

>> The '// Wait when thread really exits' is common for all the thread usage, right?
Code fragment which stops thread must work by predictable way: after it's execution we must be sure that thread doesn't exist. Code asks thread to stop (instead of killing it brutally) and waits while thread finishes. Thread must be responsive and test Stop event periodically.
BTW, after thread exists, close it's handle:

Ask thread to stop
SetEvent(hStopEvent);
// Wait when thread really exits
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
>> If I want the thread finish it by it self...

This should be in the while condition.

while (...)   // instead of ... put loop end condition

Thread can finish by usual way (all work is done) or by request to stop.
But I do not have conditions to stop the thread.
I only want sereval functions to be run in the thread.
May you give more explaination?
Consider thread that makes fixed number of calculation steps:

   for ( int i = 0; i < 1000; i++ )
   {
        // do something ...

        // test Stop event
        if ( WaitForSingleObject(hStop, 0) == WAIT_OBJECT_0 )
        {
            break;
        }
    }

This thread can exit by two ways:
1) Work is done: i = 1000
2) Thread is stopped.

Sometimes worker thread don't have exit conditions. For example, you have worker thread which listens for some port all time program is running. Such thread exits only by Stop event, this is done before program exits.

Check out MFC sample MTRECALC, this is good introduction to multithreading.
My thread is by socket trigger, so I just use as below:



long WINAPI PQ_Son(long Para)
{
do something....
if ( WaitForSingleObject(hStop, 0) == WAIT_OBJECT_0 )
{
   break;
}
}

BTW, where Can I find MTRECALC

It is part of MSDN Library, type MTRECALC in library index. However, this is calculation thread sample. If you are working with sockets, it doesn't fit for 100%.
I do not have a loop for checking of WaitForSingleObject, what should I do?
Thx a lot AlexFM
As a summary, let's start a new topic