# How to kill a thread?



## JamesElstone (May 9, 2015)

Hi All,

Just looking at an issue with a port that is driving me up the wall, and need some collective advice please!!  The question is simply about threads and not about the port in question.

A thread is successfully created by using the following code:
	
	



```
pthread_create( &tid, NULL, sub_proc, (void *) str_obj );
pthread_detach( tid );
```
In the `sub_proc` procedure it detaches itself using `pthread_detach( pthread_self() )` and there is a `pthread_exit( NULL )` if certain states are met and this works well.

In parallel to that thread is another "watchdog" thread, and if it sees certain states, it will attempt to kill the above thread.  This code in question is written for multiple platforms, but it keeps track of the thread ID and when it wants to kill it off issues a `pthread_kill(index->drain_tid, SIGABRT)`.  When the `SIGABRT` is issued the thread is in a very long `usleep` nap.

The `SIGABRT` dutifully seems to create a core dump, as per the man pages of kill(). (Although `gdb` is showing a null pointer from `usleep`) I have no idea of the behaviour on other Linux platforms, and think that this application was originally targeted at the SUN platform...

From outside of a thread, other than providing signalling into the thread via shared variables, etc, is there a way to kill off a thread within a process from another process?

All of this is in C (FreeBSD clang version 3.4.1 on FreeBSD 10.1-p9) and the syntax has been simplified in the above examples.

Also has anything changed between FreeBSD 10.0 and 10.1 in this area that could be a factor?

Thanks in advance,

James


----------



## sossego (May 10, 2015)

Kill it with kindness.


----------

