Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill a running thread

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?

like image 753
Aneesh Narayanan Avatar asked May 24 '12 12:05

Aneesh Narayanan


People also ask

How do we stop a current running 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.

How do you kill a running thread in Python?

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.

How do you kill a runnable thread?

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.

How do I stop a running thread in Linux?

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.


1 Answers

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);
like image 91
André Caron Avatar answered Nov 15 '22 23:11

André Caron