考虑以下主要部分
tcount = 0;
for (i=0; i<2; i++) {
count[i] = 0;
if (pthread_create(&tid[i], NULL, randint, (void *)&i) != 0) {
exit(1);
} else {
printf("Created Thread %d\n", i);
}
// XXXXXXX
}
for (i=0; i<nthreads; i++) {
printf("Waiting for thread %d to terminate\n", i);
pthread_join(tid[i], NULL);
}
randint 代码是:
void *randint(void *pint)
{
int j, k;
int rseed;
j = *((int *)pint);
printf("Started thread %d\n", j);
while (tcount++ < NMAX) {
count[j] += 1;
}
return(NULL);
}
输出是:
Created Thread 0
Created Thread 1
Waiting for thread 0 to terminate
Started thread 0
Started thread 0
Waiting for thread 1 to terminate
我很困惑为什么在输出中有:
Started thread 0
Started thread 0
我明白如果是:
Started thread 0
Started thread 1
或者:
Started thread 1
Started thread 1
但是2个零不清楚!!!有猜吗???