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()); pthread_exit(NULL); 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; } |
Output:
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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Function Interposition in C with an example of user defined malloc()
- Format String Vulnerability and Prevention with Example
- Named Pipe or FIFO with example C program
- ftell() in C with example
- fseek() in C/C++ with example
- pthread_cancel() in C with example
- pthread_equal() in C with example
- Command line arguments example in C
- Slack Bytes in Structures : Explained with Example
- Bug Tracking System
- C Program to sort rows of the Matrix
- Advantages and Disadvantages of Array in C
- Types of C files after its compilation
- Food Ordering System in C
Improved By : shubham_singh

