pthread_kill函数实例

pthread_kill

pthread_kill函数是用来向线程发送signal。类似于我们在linux下使用kill命令向指定的进程发送signal。
函数原型:

NAME

   pthread_kill - send a signal to a thread

SYNOPSIS

   #include <signal.h>

   int pthread_kill(pthread_t thread, int sig);

DESCRIPTION

   The  pthread_kill()  function  sends  the  signal  sig  to thread, a thread in the same process as the caller.  The signal is asyn‐
   chronously directed to thread.

   If sig is 0, then no signal is sent, but error checking is still performed.

以上是从man手册上获取的pthread_kill函数的信息,可以看出pthread_kill其实就是用来向指定的线程发送sig信号,然后执行对应的信号处理函数。

但是最后写了一个sig is 0的情况,在int sig参数为0的时候,可以用来判定线程的存活情况。

RETURN VALUE 为0,表示线程存在。
RETURN VALUE 为ESRCH,表示线程不存在。
RETURN VALUE为 EINVAL,表示信号不合法。

所以pthread_kill(thread_id,0)就可以用来检测线程的情况,来进行不同的处理。

int ret = pthread_kill(thread_id,0);
if (ret == ESRCH) {
	printf("the specified thread did not exists or already 		quit\n");
} else if (ret == EINVAL)
	printf("signal is invalid\n");
else {
	printf("the thread is alive\n");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值