호출된 thread에서 thread_id 를 구하는 방법
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t g_NONSTOP;
void *t_func(void *data)
{
int id;
id = *((int *)data);
pthread_t self = pthread_self();
printf("[in thr] pthread_self:%x\n", self);
while(1)
{
//printf("(%lu) [%d]\n",pthread_self(), id);
}
}
int main()
{
pthread_t tid;
int a = 1;
int thr_id;
int status;
thr_id = pthread_create(&tid, NULL, t_func, (void*)&a);
printf("[in main] tid:%x\n", tid);
thr_id = pthread_create(&tid, NULL, t_func, (void*)&a);
printf("[in main] tid:%x\n", tid);
if ( 0 > thr_id )
{
perror("thread create error");
exit(0);
}
pthread_join(tid, (void *)&status);
return 0;
}
컴파일
gcc -o thr thr.c -lpthread
실행 결과
root@cnode01-m:~/example# ./thr
[in main] tid:82709700
[in thr] pthread_self:82709700
[in main] tid:81f08700
[in thr] pthread_self:81f08700