-
April 16th, 2022, 10:51 AM
#1
stop a thread
a thread is running. how to cleanly stop m_thread from running regardless what it is doing.
Code:
m_thread=AfxBeginThread(RunThread, (aDlg*)this);
if (m_thread)
{
DWORD dwRet;
do
{
dwRet = :: MsgWaitForMutipleObjects(1, &m_thread->m_hThread, FALSE, INFINITE, QS_ALLINPUT);
if (dwRet != WAIT_OBJECTS_0)
{
if(::PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
}while(swRet != WAIT_OBJECT_0) && (dwRet != WAIT_FAILED));
}
m_thread = NULL;
-
April 17th, 2022, 03:26 AM
#2
Re: stop a thread
Don't reinvent the wheel! just check out a great Joe Newcomer's essay about Using Worker Threads
Victor Nijegorodov
-
April 18th, 2022, 01:12 AM
#3
Re: stop a thread
that link has good information on thread controls. However, it does not cover cases when thread is running and by click of a button, thread is terminated which is not recommended by many. Usually recommendation is to use handle and set event to signal return from thread which does not fit my case in here. terminate/stop/restart.
Last edited by @EE@; April 18th, 2022 at 01:15 AM.
-
April 18th, 2022, 02:36 AM
#4
Re: stop a thread
Originally Posted by @EE@
that link has good information on thread controls. However, it does not cover cases when thread is running and by click of a button, thread is terminated which is not recommended by many. Usually recommendation is to use handle and set event to signal return from thread which does not fit my case in here. terminate/stop/restart.
Did you read the sections:
Pausing a Thread and Thread Shutdown
Shutting down a thread from a view or main frame
Thread Shutdown Without Polling (20-Jan-01)
What does not "fit your case in here. terminate/stop/restart"?
Victor Nijegorodov
-
April 18th, 2022, 03:46 AM
#5
Re: stop a thread
You can't cleanly terminate a thread without assistance from the thread. You can terminate a thread using TerminateThread() - but that has issues. The usual way is to use event which is set by the thread wanting to terminate the thread and within the thread to be terminated, when the event is set it cleanly exits.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2022, 10:31 AM
#6
Re: stop a thread
Originally Posted by @EE@
a thread is running. how to cleanly stop m_thread from running regardless what it is doing.
To be able to stop cleanly, your thread function (RunThread, I guess) must be designed to be interruptible, which means it must loop in predictably short cycles and do some checking on every cycle if it was signaled to break. The cross-thread signaling is typically done based on a boolean variable or kernel synchronization object, like event.
So, the highlighted requirement cannot be met in general case.
Best regards,
Igor
-
April 27th, 2022, 04:18 AM
#7
Re: stop a thread
A little sample to demonstrate the approach (see my previous message)
HOWTO_interrupt_worker_thread.zip
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|