当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。