user3254491
user3254491

Reputation: 119

POSIX - pthread_kill()?

Why this thread continue its execution though I kill it??

pthread_t pid;
pthread_create(&pid, NULL, (func)countdown, NULL);
pthread_kill(pid, 1);
pthread_join(pid, NULL);

Upvotes: 1

Views: 9534

Answers (1)

merlin2011
merlin2011

Reputation: 75545

pthread_kill is a function for sending signals to a thread. You are currently sending it the signal 1, which is SIGHUP. The standard signals for requesting or forcing termination are SIGTERM and SIGKILL, which are 15 and 9 respectively.

Also, you should be using constants and not magic numbers to send signals. These are defined in signal.h. The signal documentation will give you more details.

Update: Per nos's comment, the correct way to terminate threads (and not the entire application) is to use pthread_cancel. The usage of pthread_kill I described above will tend to kill the entire process, assuming the receiving thread does not register a signal handler.

Upvotes: 3

Related Questions