pthread_kill 与pthread_cancel使用方法
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void* thread0(void* arg)
{
pthread_mutex_lock(&mutex);
printf("in thread 0 tag 1\n");
pthread_cond_wait(&cond, &mutex);
printf("in thread 0 tag 2\n");
pthread_mutex_unlock(&mutex);
printf("in thread 0 tag 3\n");
pthread_exit(NULL);
}
void* thread1(void* arg)
{
sleep(10);
printf("in thread 1 tag 1\n");
pthread_mutex_lock(&mutex);
printf("in thread 1 tag 2\n");
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);
printf("in thread 1 tag 3\n");
pthread_exit(NULL);
}
int main()
{
pthread_t tid[2];
if (pthread_create(&tid[0], NULL, thread0, NULL) != 0)
{
exit(1);
}
if (pthread_create(&tid[1], NULL, thread1, NULL) != 0)
{
exit(1);
}
sleep(5);
printf("in main thread tag 1\n");
pthread_cancel(tid[0]);
pthread_join(tid[0], NULL);
printf("tid0\n");
pthread_join(tid[1], NULL);
printf("tid1\n");
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
使用pthread_cancel 结果如下:
in thread 0 tag 1
in main thread tag 1
tid0
in thread 1 tag 1
pthread_cancel kill 掉线程,但是如果有类似拿锁的操作,则要等拿到锁才能kill掉。
pthread_kill 结果如下:
in thread 0 tag 1
in main thread tag 1
Quit
pthread_kill 会对线程0发出signal_quit(3)的系统信号。 如果线程0不处理,则整个进程退出。