當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C語言 pthread_self()用法及代碼示例


先決條件:C語言中的多線程

語法:-pthread_t pthread_self(void);

pthread_self()函數返回在其中調用該線程的ID。

// 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 function \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; 
}


輸出:


In function
thread id = 1
In main
thread id = 1


相關用法


注:本文由純淨天空篩選整理自 pthread_self() in C with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。