turbot_yu
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What details?
ASKER
I have create a thread by
hThread = CreateThread(NULL,0,(LPTHR EAD_START_ ROUTINE)PQ _Son,NULL, 0,&iID);
long WINAPI PQ_Son(long Para)
{
}
May I know how to close it?
hThread = CreateThread(NULL,0,(LPTHR
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,(LPTHR EAD_START_ ROUTINE)PQ _Son,NULL, (LPVOID)hStopEvent ,&iID);
Stop thread:
// Ask thread to stop
SetEvent(hStopEvent);
// Wait when thread really exits
WaitForSingleObject(hThrea d, 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;
}
HANDLE hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
hThread = CreateThread(NULL,0,(LPTHR
Stop thread:
// Ask thread to stop
SetEvent(hStopEvent);
// Wait when thread really exits
WaitForSingleObject(hThrea
Thread function:
long WINAPI PQ_Son(long Para)
{
HANDLE hStop = (HANDLE)Para;
while (...)
{
// do something ...
// test Stop event
if ( WaitForSingleObject(hStop,
{
break;
}
}
return 0;
}
ASKER
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.
That is all the thread need to wait to close? After it really finish.
ASKER
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
And I can choose to use 'ask to stop' or 'wait to stop'.
Am I right?
Thanks,
Turbot
ASKER
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
And I can choose to use 'ask to stop' or 'wait to stop'.
Am I right?
Thanks,
Turbot
ASKER
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;
}
}
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,
{
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(hThrea d, INFINITE);
CloseHandle(hThread);
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(hThrea
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.
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.
ASKER
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?
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.
for ( int i = 0; i < 1000; i++ )
{
// do something ...
// test Stop event
if ( WaitForSingleObject(hStop,
{
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.
ASKER
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
long WINAPI PQ_Son(long Para)
{
do something....
if ( WaitForSingleObject(hStop,
{
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%.
ASKER
I do not have a loop for checking of WaitForSingleObject, what should I do?
Thx a lot AlexFM
Thx a lot AlexFM
ASKER
As a summary, let's start a new topic
ASKER
Thanks,
Turbot