#include<pthread.h>
#include<stdio.h>
void *inc_x(void *x_void_ptr)
{
int *x_ptr=(int *)x_void_ptr;
while(++(*x_ptr)<100);
printf("x increment finished\n");
sleep(2);
return NULL;
}
int main()
{
int x = 0;
int y = 0;
printf("x:%d,y:%d\n",x,y);
pthread_t inc_x_thread;
if(pthread_create(&inc_x_thread,NULL,inc_x,&x))
{
fprintf(stderr,"Error creating thread\n");
return 1;
}
while(++y<100);
printf("y increment finished\n");
if(pthread_join(inc_x_thread, NULL))
{
fprintf(stderr,"Error joining thread\n");
return 2;
}
printf("x:%d,y:%d\n",x,y);
return 0;
}
线程的创建与使用
最新推荐文章于 2022-05-17 01:00:00 发布