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

- 347,512
- 102
- 1,199
- 985

- 17,325
- 27
- 86
- 108
5 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.

- 25,136
- 3
- 52
- 71
-
4The problem with pthread_cancel, btw, is that if you use it, then the code running in the thread that's cancelled, has to understand cancellation points, and make sure that (a) it hits one frequently enough that it actually gets cancelled in good time, and (b) it doesn't leak resources when this happens. This is kind of tricky, whereas if you use a message, then it's nicely explicit in your code exactly when the thread can exit. – Steve Jessop Jan 18 '10 at 11:07
-
16@Steve: Unfortunately, `pthread_cancel` is the only clean way *in library code* to interrupt a thread that might be blocked on a syscall. The other method is to install an interrupting signal handler and be sure all of your code is prepared to deal with `EINTR`, but library code cannot assume it has a right to change signal handling or impose `EINTR` handling on the caller. – R.. GitHub STOP HELPING ICE Mar 24 '11 at 17:08
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 usingpthread_kill()
and wait for its termination withpthread_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 withpthread_join()
. This approach requires detailed usage ofpthread_cleanup_push()
andpthread_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.

- 4,882
- 2
- 29
- 55
I agree with Antti, better practice would be to implement some checkpoint(s) where the thread checks if it should terminate. These checkpoints can be implemented in a number of ways e.g.: a shared variable with lock or an event that the thread checks if it is set (the thread can opt to wait zero time).

- 3,764
- 3
- 30
- 33
-
1In most general cases this absolutely correct. However, there are many situations in which this is simply not practical. For example, my current project is a minimal text editor, in which I have a dedicated thread for receiving user input. Since `getch()` operates synchronously, my input thread would be unable to detect that it needs to terminate until the user pressed another key. This would be very bad design. Since the user input thread never needs to allocate additional resources, it is far better to kill it with `pthread_cancel()`. – May 11 '20 at 23:02
pthread_exit(0)
This will kill the thread.

- 224
- 2
- 10
-
1This answer is wrong. pthread_exit() is a function used to terminate the thread where this function is called. This is a proper termination. pthread_exit() cannot be used to affect any other thread. Kindly don't mislead. – Animesh Kumar Oct 25 '20 at 17:37