Linux下使用pthread

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
 
void *thrd_func(void *arg);
pthread_t tid;
  
int main(){
      if (pthread_create(&tid,NULL,thrd_func,NULL)!=0) {
         printf("Create thread error!\n");
         exit(1);
     }
 
     printf("TID in pthread_create function: %u.\n",tid);
     printf("Main process: PID: %d,TID: %u.\n",getpid(),pthread_self()); 
     
     sleep(1); //race
 
     return 0;
 }
 
 void *thrd_func(void *arg){
     printf("New process:  PID: %d,TID: %u.\n",getpid(),pthread_self()); 
     printf("New process:  PID: %d,TID: %u.\n",getpid(),tid); 
     pthread_exit(NULL); 
}



 gcc -o pthreadTest1 pthreadTest1.c -lpthread

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章