>  기사  >  백엔드 개발  >  C 언어에서 pthread_self()는 현재 스레드의 ID를 가져오는 것을 의미합니다.

C 언어에서 pthread_self()는 현재 스레드의 ID를 가져오는 것을 의미합니다.

WBOY
WBOY앞으로
2023-09-18 15:21:041219검색

C 언어에서 pthread_self()는 현재 스레드의 ID를 가져오는 것을 의미합니다.

여기서 C 언어에서 pthread_self()의 역할을 살펴보겠습니다. pthread_self() 함수는 현재 스레드의 ID를 얻는 데 사용됩니다. 이 함수는 기존 스레드를 고유하게 식별합니다. 그러나 여러 스레드가 있고 하나의 스레드가 완료되면 ID를 재사용할 수 있습니다. 따라서 ID는 실행 중인 모든 스레드에 대해 고유합니다.

#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

위 내용은 C 언어에서 pthread_self()는 현재 스레드의 ID를 가져오는 것을 의미합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제