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 funtion \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; } |
chevron_right
filter_none
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.
Recommended Posts:
- asctime() and asctime_s() functions in C with Examples
- time.h header file in C with Examples
- __builtin_inf() functions of GCC compiler
- Count substrings that contain all vowels | SET 2
- How can we use Comma operator in place of curly braces?
- Basic Code Optimizations in C
- Constants vs Variables in C language
- Sum of an array using MPI
- Difference between pointer to an array and array of pointers
- dot (.) operator in C/C++
- Features and Use of Pointers in C/C++
- Difference between while and do-while loop in C, C++, Java
- C program to sort an array using pointers
- C Program to count the Number of Characters in a File
- Analyzing BufferOverflow with GDB



