Home >Backend Development >C++ >In C language, pthread_self() means to get the ID of the current thread
Here we look at the role of pthread_self() in C language. The pthread_self() function is used to obtain the ID of the current thread. This function uniquely identifies an existing thread. But if there are multiple threads, and one thread completes, then the id can be reused. Therefore, the id is unique for all running threads.
#include <stdio.h> #include <stdlib.h> #include <pthread.h> void* func(void* p) { printf("From the function, the thread id = %d</p><p>", 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</p><p>", thread); pthread_join(thread, NULL); //join with main thread }
From the main function, the thread id = 1 From the function, the thread id = 1
The above is the detailed content of In C language, pthread_self() means to get the ID of the current thread. For more information, please follow other related articles on the PHP Chinese website!