您的位置首页百科知识

如何不使用pthread

如何不使用pthread

的有关信息介绍如下:

如何不使用pthread

如何不使用pthread_cancel而杀死线程

During the time I use standalone cross compliers to build my system, I find there is NO pthread_cancel in pthread.h (/home/dengwei/standalone-toolchain/sysroot/usr/include/pthread.h).

Shocked by that, but here comes the solution, by using pthread_kill to send a signal , and adding a signal handler :

#include

#include

#include

#include

#include

#include

#include

pthread_t pid;

void handle_quit(int signo)

{

printf("in qq handle sig %d \n", signo);

pthread_exit(NULL);

}

void* test(void *arg)

{

signal(SIGQUIT,handle_quit );

for(int i=0;i<100;i++)

{

printf("in pthread test \n");

sleep(1);

}

}

int main(void)

{

printf("begin \n");

pthread_create(&pid, NULL , test, NULL);

sleep(3);

if(pthread_kill(pid, 0)!= ESRCH)

{

printf("thread %d exists!\n", pid);

pthread_kill(pid, SIGQUIT);

// pthread_exit(NULL);//this won't work

printf("after kill\n");

}

sleep(1);

printf("exit in main\n");

}