C中的pthread_self()及其示例

先决条件: C语言中的多线程

null

语法:-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 thread id = %d" , pthread_self());
pthread_exit(NULL);
return NULL;
}
int main()
{
pthread_t thread ; // declare thread
pthread_create(& thread , NULL, calls, NULL);
printf ( "In main thread id = %d" , thread );
pthread_join( thread , NULL);
return 0;
}


输出:

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

本文由 德万舒阿加瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享