线程的创建pthread_create.c

时间:2023-03-08 21:55:10
线程的创建pthread_create.c
 #include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <errno.h> void *pthread_fun(void *arg)
{
int b;
b = *(int *)arg;
printf("b = %d \n",b);
int i = ;
while(i > )
{
printf("pthread start \n");
sleep();
i -- ;
}
}
int main()
{
pthread_t pthread;
int a =;
#if 0
if (pthread_create(&pthread,NULL,pthread_fun,NULL) < )
{
perror("fail to pthread_create");
exit();
}
#endif
#if 1
if (pthread_create(&pthread,NULL,pthread_fun,&a) < )
{
perror("fail to pthread_create");
exit();
}
#endif
printf("pthread create success\n");
pthread_join(pthread,NULL);//等待线程的退出
printf("pthread exit \n");
return ;
}