Wednesday, February 4, 2009

get thread id

#include
#include
#include


int my_gettid()
{
int * p = (int*)pthread_self();
int pid = getpid();
assert(pid == p[19]);
return p[18];
}

int my_gettid(pthread_t thread)
{
int *p = (int*)thread;
return p[18];
}

void BusyWork(void* t)
{
int tid = my_gettid();
int pid = getpid();
printf("worker tid %d pid %d\n",tid,pid);
}

int main()
{
int tid = my_gettid();
int pid = getpid();
printf("main tid %d pid %d\n",tid,pid);

pthread_t thread;
pthread_create(&thread, NULL, BusyWork, NULL);

pthread_join(thread,NULL);
return 0;
}

No comments:

Post a Comment