不提倡强制杀死线程,当我们的一个线程获取了一个锁,正在访问某个共享方法的时候,还没来得及解锁就被干掉了,那这个锁就永远不会被解掉了,于是所有依赖这个锁的其它线程可能就锁死了。
android的ndk中没有提供类似linux的pthread_cancel函数来杀死线程。
#include <jni.h>
#include <string>
#include<pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include "Logger.h"
pthread_t pid;
void handle_quit(int signo) {
LOGE("hxk>>>in qq handle sig!\n");
pthread_exit(0);
}
void *test(void *arg) {
signal(SIGTERM, handle_quit);
for (int i = 0; i < 100; i++) {
LOGE("hxk>>>in pthread test \n");
sleep(1);
}
}
void test1() {
pthread_create(&pid, NULL, test, NULL);
sleep(3);
if (pthread_kill(pid, 0) != ESRCH) {
LOGE("hxk>>>thread %d exists!\n");
pthread_kill(pid, SIGTERM);
//