Threads
-
Programming wth threads is not easy as many newbies think. A few years back my company was downsized and I had to take ownership of some code written by another programmer. The first thing I had to fix was some resource leaks when a given task was canceled. The task itself happened on a worker thread. The UI thread had a cacel button to cancel the task. After examining the code, I found the following:
void CTaskProgessDialog::OnCancel()
{
TerminateThread(m_pWorkThread->m_hHandle);
}void CWorkerThread::DoTask()
{
AcquireResources();// code that does something
ReleaseResources();
} -
Programming wth threads is not easy as many newbies think. A few years back my company was downsized and I had to take ownership of some code written by another programmer. The first thing I had to fix was some resource leaks when a given task was canceled. The task itself happened on a worker thread. The UI thread had a cacel button to cancel the task. After examining the code, I found the following:
void CTaskProgessDialog::OnCancel()
{
TerminateThread(m_pWorkThread->m_hHandle);
}void CWorkerThread::DoTask()
{
AcquireResources();// code that does something
ReleaseResources();
} -
Programming wth threads is not easy as many newbies think. A few years back my company was downsized and I had to take ownership of some code written by another programmer. The first thing I had to fix was some resource leaks when a given task was canceled. The task itself happened on a worker thread. The UI thread had a cacel button to cancel the task. After examining the code, I found the following:
void CTaskProgessDialog::OnCancel()
{
TerminateThread(m_pWorkThread->m_hHandle);
}void CWorkerThread::DoTask()
{
AcquireResources();// code that does something
ReleaseResources();
}As mentioned the termination of a thread should be internal, not with a Terminate command. Set a flag and wait for self-termination. You never know if a thread has a socket or file open, or any other resources. You can't simply say, no thread can ever aquire resources, and with multi-core and multi-processor allowing two simultaneous operations, it is even more difficult to judge if a terminate signal will work without memory leak even with back-to-back aquire()/release() because the terminate can occur in the middle of aquiring, before releasing, or in the middle of releasing. Terminates should all be handled gracefully from inside. The "code that does something" needs to check for a terminate early flag and release resources (if any) and go on. Simply putting aquire/release back to back will not save you. This is a common failure even with experts on threading thinking that back-to-back operations will not be interrupted. It's all a roll of the dice, back to back operations may make you think everything is okay because chances are reduced, but eventually it will show up as a problem, and then it will be difficult to repeat it for debugging. remember the core premise of threading: thou shalt not commit murder, all threads should be directed to suicide. ;)
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
-
As mentioned the termination of a thread should be internal, not with a Terminate command. Set a flag and wait for self-termination. You never know if a thread has a socket or file open, or any other resources. You can't simply say, no thread can ever aquire resources, and with multi-core and multi-processor allowing two simultaneous operations, it is even more difficult to judge if a terminate signal will work without memory leak even with back-to-back aquire()/release() because the terminate can occur in the middle of aquiring, before releasing, or in the middle of releasing. Terminates should all be handled gracefully from inside. The "code that does something" needs to check for a terminate early flag and release resources (if any) and go on. Simply putting aquire/release back to back will not save you. This is a common failure even with experts on threading thinking that back-to-back operations will not be interrupted. It's all a roll of the dice, back to back operations may make you think everything is okay because chances are reduced, but eventually it will show up as a problem, and then it will be difficult to repeat it for debugging. remember the core premise of threading: thou shalt not commit murder, all threads should be directed to suicide. ;)
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
Sure! I can't believe how many programmers (even experienced ones) don't know this.
-
As mentioned the termination of a thread should be internal, not with a Terminate command. Set a flag and wait for self-termination. You never know if a thread has a socket or file open, or any other resources. You can't simply say, no thread can ever aquire resources, and with multi-core and multi-processor allowing two simultaneous operations, it is even more difficult to judge if a terminate signal will work without memory leak even with back-to-back aquire()/release() because the terminate can occur in the middle of aquiring, before releasing, or in the middle of releasing. Terminates should all be handled gracefully from inside. The "code that does something" needs to check for a terminate early flag and release resources (if any) and go on. Simply putting aquire/release back to back will not save you. This is a common failure even with experts on threading thinking that back-to-back operations will not be interrupted. It's all a roll of the dice, back to back operations may make you think everything is okay because chances are reduced, but eventually it will show up as a problem, and then it will be difficult to repeat it for debugging. remember the core premise of threading: thou shalt not commit murder, all threads should be directed to suicide. ;)
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
"thou shalt not commit murder, all threads should be directed to suicide" :) Nice.
Need Another Seven Acronyms...
Confused? You will be...