यहां हम देखेंगे कि c में pthread_self () का क्या प्रभाव होगा। pthread_self () फ़ंक्शन का उपयोग वर्तमान थ्रेड की आईडी प्राप्त करने के लिए किया जाता है। यह फ़ंक्शन विशिष्ट रूप से मौजूदा थ्रेड्स की पहचान कर सकता है। लेकिन अगर कई धागे हैं, और एक धागा पूरा हो गया है, तो उस आईडी का पुन:उपयोग किया जा सकता है। तो सभी चल रहे थ्रेड्स के लिए, आईडी अद्वितीय हैं।
उदाहरण
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* func(void* p) {
printf("From the function, the thread id = %d\n", pthread_self()); //get current thread id
pthread_exit(NULL);
return NULL;
}
main() {
pthread_t thread; // declare thread
pthread_create(&thread, NULL, func, NULL);
printf("From the main function, the thread id = %d\n", thread);
pthread_join(thread, NULL); //join with main thread
} आउटपुट
From the main function, the thread id = 1 From the function, the thread id = 1