⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 12-6.c

📁 提供了许多线程间通信的实例程序。用linux下的C语言实现。
💻 C
字号:
#include<stdio.h>
 #include<pthread.h>
 pthread_once_tonce=PTHREAD_ONCE_INIT;
 void once_run(void)
  {        
    printf("once_run in thread %d\n",pthread_self());
 }
  
   void * child1(void *arg)
  {      
   int tid=pthread_self();        
   printf("thread %d enter\n",tid);        
  pthread_once(&once,once_run);        
   printf("thread %d returns\n",tid);
 }
 
  void * child2(void *arg)
  {
    int tid=pthread_self();        
    printf("thread %d enter\n",tid);        
    pthread_once(&once,once_run);        
    printf("thread %d returns\n",tid);
  }

  int main(void)
  {        
     int tid1,tid2;       
    printf("hello\n");
    pthread_create(&tid1,NULL,child1,NULL); 
    pthread_create(&tid2,NULL,child2,NULL);
    sleep(10);
    printf("main thread exit\n");        
   return 0;
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -