- 论坛徽章:
- 0
|
创建了四个线程,打印传递过去的参数。传递参数为 0,1,2,3,为何打印结果有时是1 ,2, 3, 2 呢?
——————————————————
#include <stdio.h>
#include <pthread.h>
void * prinfthread(int *arg)
{
int *num;
num = (int *)arg;
printf("this is thread:%d\n",*num);
}
int main(int argc,char *argv[])
{
pthread_t pth[4];
int *attr ;
int i,j;
printf("this is main.\n");
for(i = 0;i<4;i++)
{
attr = &i;
pthread_create(&pth,NULL,(void *)prinfthread,(void *)attr);
}
for(j =0;j<4;j++)
{
pthread_join(pth[j],NULL);
}
return 0;
}
————————————————————
SuSE9Sp2:/myproject/lesson # ./thread
this is main.
this is thread:1
this is thread:2
this is thread:3
this is thread:2
SuSE9Sp2:/myproject/lesson # ./thread
this is main.
this is thread:1
this is thread:2
this is thread:3
this is thread:4 |
|