Here we will see what will be the effect of pthread_self() in C. The pthread_self() function is used to get the ID of the current thread. This function can uniquely identify the existing threads. But if there are multiple threads, and one thread is completed, then that id can be reused. So for all running threads, the ids are unique.
Example
#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 }
Output
From the main function, the thread id = 1 From the function, the thread id = 1