C 中的 pthread_self()
在这里我们将看到pthread_self()在C中会产生什么效果。该pthread_self()函数用于获取当前线程的ID。该函数可以唯一标识现有线程。但是如果有多个线程,一个线程完成了,那么那个id就可以复用了。所以对于所有正在运行的线程,id是唯一的。
示例
#include输出结果#include #include void* func(void* p) { printf("From the function, the thread id = %d\n", pthread_self()); //获取当前线程ID pthread_exit(NULL); return NULL; } main() { pthread_t thread; //声明线程 pthread_create(&thread, NULL, func, NULL); printf("From the main function, the thread id = %d\n", thread); pthread_join(thread, NULL); //加入主线程 }
From the main function, the thread id = 1 From the function, the thread id = 1
热门推荐
10 读书祝福语女生短句简短
11 感谢同事新婚祝福语简短
12 感谢教官文案祝福语简短
13 员工聚会蛋糕祝福语简短
14 元宵发客户祝福语简短
15 给团队信心祝福语简短
16 医美的祝福语大全简短
17 异国姻缘的祝福语简短
18 兄妹拍照祝福语简短的话