pthread_self() in C with Example

Prerequisite : Multithreading in C

Syntax :- pthread_t pthread_self(void);

The pthread_self() function returns the ID of the thread in which it is invoked.





// C program to demonstrate working of pthread_self()
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* calls(void* ptr)
    // using pthread_self() get current thread id
    printf("In function \nthread id = %d\n", pthread_self());
    return NULL;
int main()
    pthread_t thread; // declare thread
    pthread_create(&thread, NULL, calls, NULL);
    printf("In main \nthread id = %d\n", thread); 
    pthread_join(thread, NULL); 
    return 0;



In function
thread id = 1
In main
thread id = 1

This article is contributed by Devanshu Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : shubham_singh

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.