invalid conversion from ‘__pthread_t*’ to ‘pid_t’

编译一份源码,提示有错误

error: invalid conversion from ‘__pthread_t*’ to ‘pid_t’

initializing argument 1 of ‘int kill(pid_t, int);

定位到源码

kill((p->_pNodeBuf[p->_cur_num-1])._th_id,SIGKILL);

 

即kill的参数类型为pid_t,而实际上传递的为pthread_t,这种情况下gcc报错提示无法转换。

 

在网上搜索到有个函数

pthread_kill(pthread_t pid,int sig);

替换下即可。

但是有个问题,pthread_t 和pid_t 有什么关系?

pid_t是进程的标识,实际是个unsigned int类型;

pthread_t是线程的标识,它是一个非可移植性的类型,也就是说,在这个系统中,可能是unsigned int类型,在别的系统可能是long,double,或者甚至就是个结构体。

但glibc提供了pthread_t的封装,有两个函数

线程ID,还是提供了一些可以移植性的函数的
1.线程通过调用pthread_self()函数获取自身的线程ID号
     pthread_t pthread_self(void);
2.比较两个线程的ID号
     int pthread_equal(pthread_t tid1,pthread_t tid2);

还有一种获取线程id的方式,gettid,但

The man page for gettid says:
The thread ID returned by this call is not the same thing as a POSIX thread ID

有兴趣的人,可以做下测试。或者参照《在linux上获得线程id的方法》提供的三种方法。

posted @ 2012-02-25 14:38  westfly  阅读(874)  评论(0编辑  收藏  举报