4

Possible Duplicate:
kill thread in pthread

Here after a source code containing a launch of thread and then after a while I want to kill it. How to do it ? Without make any change in the thread function

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

pthread_t test_thread;

void *thread_test_run (void *v) // I must not modify this function
{
    int i=1;
    while(1)
    {
       printf("into thread %d\r\n",i);
       i++; 
       sleep(1);
    }
    return NULL
}

int main()
{
    pthread_create(&test_thread, NULL, &thread_test_run, NULL);

    sleep (20);
    // Kill the thread here. How to do it?

   // other function are called here...

    return 0;
}
Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 3
    http://stackoverflow.com/questions/2084830/kill-thread-in-pthread – David Ranieri Nov 08 '12 at 08:44
  • 3
    Note that it is a bad idea to bluntly "kill" threads. Each thread should be properly designed with a way to stop it from the main program. Just because there exists functions to kill threads, doesn't mean that you should start design your thread functions in a poor manner. – Lundin Nov 08 '12 at 08:47
  • 1
    I would suggest looking at `pthread_join` and `pthread_attr_setdetachstate`... make some mechanism to tell the thread that it needs to exit now and call `pthread_join` on the thread to cleanup stuff used. –  Nov 08 '12 at 08:51
  • I m using a function from an external lib and this function contains infinite while loop. this functions make changes on a global variable 1 time in a random time. so I want to kill the thread after the global variable get changed. I do not need the thread running any more – MOHAMED Nov 08 '12 at 08:52
  • is it ppossible that the `pthread_cancel(pthread_t thread);` cause a block if the thread function is executing a system call – MOHAMED Nov 08 '12 at 08:55

2 Answers2

15

You can use pthread_cancel() to kill a thread:

int pthread_cancel(pthread_t 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. From man pthread_cleanup_push(3):

These functions manipulate the calling thread's stack of thread-cancellation clean-up handlers. A clean-up handler is a function that is automatically executed when a thread is cancelled (or in various other circumstances described below); it might, for example, unlock a mutex so that it becomes available to other threads in the process.

Regarding the question of whether a thread will be cancelled if blocking or not, it's not guaranteed, note that the manual also mentions this:

A thread's cancellation type, determined by pthread_setcanceltype(3), may be either asynchronous or deferred (the default for new threads). Asynchronous cancelability means that the thread can be canceled at any time (usually immediately, but the system does not guarantee this). Deferred cancelability means that cancellation will be delayed until the thread next calls a function that is a cancellation point.

So this means that the only guaranteed behaviour is the the thread will be cancelled at a certain point after the call to pthread_cancel().

Note: If you cannot change the thread code to add the cleanup handlers, then your only other choice is to kill the thread with pthread_kill(), however this is a very bad way to do it for the aforementioned reasons.

iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • 2
    +1 for mentioning about pthread_cleanup_push() – CCoder Nov 08 '12 at 08:51
  • is it ppossible that the `pthread_cancel(pthread_t thread);` cause a block if the thread function is executing a system call? – MOHAMED Nov 08 '12 at 08:57
  • @MohamedKALLEL No, a call to `pthread_cancel()` will never be a blocking call. – alk Nov 08 '12 at 09:12
  • @MohamedKALLEL I miss understood your question, no pthread_cancel won't block, but if the thread is blocking on another call, read for example, there's no guarantee that it will be cancelled immediately – iabdalkader Nov 08 '12 at 09:14
  • @iabdalkader, did your text imply that a thread being `pthread_cancel` will only cancel when it reaches a cancellation point and not while it is executing a blocking function (and a cancellation point) ? This seems not true, as a thread doing `sleep` or `select` will cancel immediately, it does not have to reach the next cancellation point/function call. – user1502776 Jun 07 '19 at 09:35
4

The short answer is this: With the cooperation of the code that thread is running, you may terminate it using any method it supports. Without the cooperation of the code that thread is running, you shouldn't even try it. What if the thread is holding a critical lock when you kill it? What if the thread has allocated memory and there is no other code to free that memory?

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Your answer works if and only if the code that thread is running supports it. Otherwise, it could destroy the thread while it's holding a critical lock or after it had allocated memory. As I said, if the code supports it, you can use any method it supports. But if you don't know for a fact that the code supports this, you shouldn't even try it. – David Schwartz Nov 08 '12 at 09:20
  • yes, that's the purpose of `pthread_cleanup_push` to add the code that supports cancelling the thread. – iabdalkader Nov 08 '12 at 09:23
  • Right, but his question states that he can't change the code that he's trying to terminate. So he can use this method if, and only if, the code he's trying to terminate supports it. But he could use *any* method the code he's trying to terminate supports. And if it doesn't support any method, then he can't use any. – David Schwartz Nov 08 '12 at 09:23
  • yes very good point, if he can't change the function's code, even to just add a call to `pthread_cleanup_push` then I see no way of doing it. – iabdalkader Nov 08 '12 at 09:26
  • I've placed a note on my answer, hope this makes it more clear. – iabdalkader Nov 08 '12 at 09:28