oncerun.c
来自「LINUX C编程实战这本书附带光盘的原代码」· C语言 代码 · 共 37 行
C
37 行
#include <stdio.h>
#include <pthread.h>
pthread_once_t once = PTHREAD_ONCE_INIT;
void run(void)
{
printf("Fuction run is running in thread %d\n",pthread_self());
}
void * thread1(void *arg)
{
pthread_t thid=pthread_self();
printf("Current thread's ID is %d\n", thid);
pthread_once(&once,run);
printf("thread1 ends\n");
}
void * thread2(void *arg)
{
pthread_t thid=pthread_self();
printf("Current thread's ID is %d\n", thid);
pthread_once(&once, run);
printf("thread2 ends\n");
}
int main(void)
{
pthread_t thid1,thid2;
pthread_create(&thid1,NULL,thread1,NULL);
pthread_create(&thid2,NULL, thread2,NULL);
sleep(3);
printf("main thread exit! \n");
exit(0);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?