#include <pthread.h>
#include <stdio.h>
#include <string.h>
pthread_t tid1,tid2,tid3;
void* pthread_01(void* arg ){
printf("线程函数1\n");
return 0;//不返回值 就报错
}
void* pthread_02(){
printf("线程函数2\n");
return 0;
}
void* pthread_03(){
printf("线程函数3\n");
return 0;
}
int test04(void) {
pthread_create(&tid1,NULL,pthread_01,NULL);
pthread_create(&tid2,NULL,pthread_02,NULL);
pthread_create(&tid3,NULL,pthread_03,NULL);
pthread_join(tid1,NULL);//不使用pthread_join看不到线程函数的输出。
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
return 0;
}
----------------------------------
#include "test.h"
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
void *thr_run(){
int i = 0;
while (i< 5) {
i++;
printf("cnt=%d this is thr_run\n",i);
sleep(3);
}
return NULL;
}
int init(){
int ret;
pthread_t tid;
ret = pthread_create(&tid,NULL,thr_run,NULL);
if(ret){
printf("pthread_create err\n");
return -1;
}
double result = sqrt(4.0);
printf("this is math result = %lf\n",result);
pthread_join(tid,NULL);
printf("Bye!\n");
return 0;
}