一 代码
#include <iostream>
#include <pthread.h>
#include <signal.h>
#include <unistd.h> //sleep
#include "errno.h"
using namespace std;
void *thfunc(void *arg) // 线程函数
{
int tm = 50;
while (1)
{
cout << "thrfunc--left:"<<tm<<" s--" <<endl;
sleep(1);
tm--;
}
return (void *)0;
}
int main(int argc, char *argv[])
{
pthread_t pid;
int res;
res = pthread_create(&pid, NULL, thfunc, NULL); // 创建线程
sleep(5);
int kill_rc = pthread_kill(pid, 0); // 发送信号0,探测线程是否存活
// 打印探测结果
if (kill_rc == ESRCH)
cout<<"the specified thread did not exists or already quit\n";
else if (kill_rc == EINVAL)
cout<<"signal is invalid\n";
else
cout<<"the specified thread is alive\