zoukankan      html  css  js  c++  java
  • 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的方法》提供的三种方法。

  • 相关阅读:
    论文笔记4
    论文笔记3
    论文笔记2
    论文笔记1
    论文笔记
    AFG与AWG的比较
    Linux下“有线线缆被拔出”问题的解决
    python生成excel格式座位表
    PythonTip--一马当先--bfs
    python pygame--倒计时
  • 原文地址:https://www.cnblogs.com/westfly/p/2367766.html
Copyright © 2011-2022 走看看