Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill Thread in Pthread Library

I use pthread_create(&thread1, &attrs, //... , //...); and need if some condition occured need to kill this thread how to kill this ?

like image 826
Sajad Bahmani Avatar asked Jan 18 '10 09:01

Sajad Bahmani


People also ask

How do you kill a thread in pthread?

The pthread_exit() function terminates the calling thread, making its exit status available to any waiting threads. Normally, a thread terminates by returning from the start routine that was specified in the pthread_create() call which started it.

How do you terminate a thread?

A thread automatically terminates when it returns from its entry-point routine. A thread can also explicitly terminate itself or terminate any other thread in the process, using a mechanism called cancelation.

Does pthread join kill thread?

The pthread_join() function shall suspend execution of the calling thread until the target thread terminates, unless the target thread has already terminated.

How do you kill a thread in Linux?

You can't only kill a thread of a process, if you use the command "kill -9 threadNo", you will kill the process.


2 Answers

First store the thread id

pthread_create(&thr, ...) 

then later call

pthread_cancel(thr) 

However, this not a recommended programming practice! It's better to use an inter-thread communication mechanism like semaphores or messages to communicate to the thread that it should stop execution.

Note that pthread_kill(...) does not actually terminate the receiving thread, but instead delivers a signal to it, and it depends on the signal and signal handlers what happens.

like image 108
Antti Huima Avatar answered Sep 25 '22 17:09

Antti Huima


There are two approaches to this problem.

  • Use a signal: The thread installs a signal handler using sigaction() which sets a flag, and the thread periodically checks the flag to see whether it must terminate. When the thread must terminate, issue the signal to it using pthread_kill() and wait for its termination with pthread_join(). This approach requires pre-synchronization between the parent thread and the child thread, to guarantee that the child thread has already installed the signal handler before it is able to handle the termination signal;
  • Use a cancellation point: The thread terminates whenever a cancellation function is executed. When the thread must terminate, execute pthread_cancel() and wait for its termination with pthread_join(). This approach requires detailed usage of pthread_cleanup_push() and pthread_cleanup_pop() to avoid resource leakage. These last two calls might mess with the lexical scope of the code (since they may be macros yielding { and } tokens) and are very difficult to maintain properly.

(Note that if you have already detached the thread using pthread_detach(), you cannot join it again using pthread_join().)

Both approaches can be very tricky, but either might be specially useful in a given situation.

like image 33
alecov Avatar answered Sep 24 '22 17:09

alecov