pthread_self() ফাংশনটি বর্তমান থ্রেডের ID রিটার্ন করে। এটি প্রতিটি রানিং থ্রেডকে অনন্যভাবে সনাক্ত করে। কিন্তু একটি থ্রেড 완료 হলে তার ID পুনরব্যবহার হতে পারে। তাই সমস্ত রানিং থ্রেডের ID ইউনিক থাকে।
উদাহরণ
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* func(void* p) {
printf("From the function, the thread id = %d\n", pthread_self());
pthread_exit(NULL);
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, func, NULL);
printf("From the main function, the thread id = %d\n", thread);
pthread_join(thread, NULL);
return 0;
}
আউটপুট
From the main function, the thread id = 1 From the function, the thread id = 1