What happens if we forcefully kill a running thread
I have a thread namely RecordThread()
which calls some complex and time consuming functions. In these functions I am using try-catch blocks, allocating and deallocation memory and using critical section variables etc.
like
void RecordThread()
{
AddRecord();
FindRecord();
DeleteRecord();
// ...
ExitThread(0);
}
After creating this thread, I am immediately killing it before the thread completes its execution. In this case what happens if the thread is forcefully killed? Do the internal functions (AddRecord
, DeleteRecord
) complete their execution after we killed the thread?
Whenever we want to stop a thread from running state by calling stop() method of Thread class in Java. This method stops the execution of a running thread and removes it from the waiting threads pool and garbage collected. A thread will also move to the dead state automatically when it reaches the end of its method.
In Python, you simply cannot kill a Thread directly. If you do NOT really need to have a Thread (!), what you can do, instead of using the threading package , is to use the multiprocessing package . Here, to kill a process, you can simply call the method: yourProcess.
But if we want to stop a thread from running or runnable state, we will need to calling stop() method of Thread class. The stop() method is generally used when we desire premature death of a thread. Since the stop() method is static in nature, therefore, it can be called by using Thread class name.
You can use pthread_cancel() to kill a thread: Note that the thread might not get a chance to do necessary cleanups, for example, release a lock, free memory and so on.. So you should first use pthread_cleanup_push() to add cleanup functions that will be called when the thread is cancelled.
After creating this thread, I am immediately killing it before the thread completes its execution.
I assume you mean you are using TerminateThread()
in the following fashion:
HANDLE thread = CreateThread(...);
// ...
// short pause or other action?
// ...
TerminateThread(thread, 0); // Dangerous source of errors!
CloseHandle(thread);
If that is the case, then no, the thread executing RecordThread()
will be stopped exactly where it is at the time that the other thread calls TerminateThread()
. As per the notes in the TerminateThread()
documentation, this exact point is somewhat random and depends on complex timing issues which are out of your control. This implies that you can't handle proper cleanup inside a thread and thus, you should rarely, if ever, kill a thread.
The proper way to request the thread to finish is by using WaitForSingleObject()
like so:
HANDLE thread = CreateThread(...);
// ...
// some other action?
// ...
// you can pass a short timeout instead and kill the thread if it hasn't
// completed when the timeout expires.
WaitForSingleObject(thread, INFINITE);
CloseHandle(thread);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With