
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
pthread_self() in C
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
", 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
", 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
Advertisements