pthread_self返回的是POSIX phtread库在用户空间标识的线程id,与linux调度任务没有关系,且只能保证同一进程在同一时间的线程具有不同的id,换句话说就是同一进程在不同时间先后运行的线程可能具有相同的id:
#include <iostream>
#include <pthread.h>
#include <thread>
using namespace std;
void t_func(void *arg)
{
cout << "This is thread:" << *(int*)arg << ", pthread_self id:"<< pthread_self() <<endl;
}
int main()
{
int i = 1;
thread t1(t_func, &i);
t1.join();
++i;
thread t2(t_func, &i);
t2.join();
return 0;
}
运行程序输出:
This is thread:1, pthread_self id:139920145725184
This is thread:2, pthread_self id:139920145725184
可见先后运行的两个线程具有相同的pthread_self id
gettid需要用户自己在封装一下ÿ