C++ gettid() was not declared in this scope

前端 未结 3 2100
轻奢々
轻奢々 2020-12-28 17:32

A simple program is: I would like to get the thread ID of both of the threads using this gettid function. I do not want to do the sysCall directly. I want to use this funct

相关标签:
3条回答
  • 2020-12-28 17:58

    The man page you refer to can be read online here. It clearly states:

    Note: There is no glibc wrapper for this system call; see NOTES.

    and

    NOTES

    Glibc does not provide a wrapper for this system call; call it using syscall(2).

    The thread ID returned by this call is not the same thing as a POSIX thread ID (i.e., the opaque value returned by pthread_self(3)).

    So you can't. The only way to use this function is through the syscall.

    But you probably shouldn't anyway. You can use pthread_self() (and compare using pthread_equal(t1, t2)) instead. It's possible that boost::thread has its own equivalent too.

    0 讨论(0)
  • 2020-12-28 18:04

    Additional to the solution provided by Glenn Maynard it might be appropriate to check the glibc version and only if it is lower than 2.30 define the suggested macro for gettid().

    #if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
    #include <sys/syscall.h>
    #define gettid() syscall(SYS_gettid)
    #endif
    
    0 讨论(0)
  • 2020-12-28 18:10

    This is a silly glibc bug. Work around it like this:

    #include <unistd.h>
    #include <sys/syscall.h>
    #define gettid() syscall(SYS_gettid)
    
    0 讨论(0)
提交回复
热议问题