Home > Backend Development > C++ > In C language, pthread_self() means to get the ID of the current thread

In C language, pthread_self() means to get the ID of the current thread

WBOY
Release: 2023-09-18 15:21:04
forward
1347 people have browsed it

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.

Example

#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
}
Copy after login

Output

From the main function, the thread id = 1
From the function, the thread id = 1
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template