How do I get a thread ID from an arbitrary pthread_t?

前端 未结 5 651
轮回少年
轮回少年 2020-11-27 16:13

I have a pthread_t, and I\'d like to change its CPU affinity. The problem is that I\'m using glibc 2.3.2, which doesn\'t have pthread_setaffinity_np(). That\'s OK, though, b

5条回答
  •  自闭症患者
    2020-11-27 16:34

    Actually pthread_self returns pthread_t and not a integer thread id you can work with, the following helper function will get you that in a portable way across different POSIX systems.

    uint64_t gettid() {
        pthread_t ptid = pthread_self();
        uint64_t threadId = 0;
        memcpy(&threadId, &ptid, std::min(sizeof(threadId), sizeof(ptid)));
        return threadId;
    }
    

提交回复
热议问题