I have this small program that I found in an exam subject of an OS course.
void * func (void * p) {
int n = p;
printf("%d \n",n);
return NULL;
}
int main() {
int i;
pthread_t t[3];
for(i=0; i<3; i+=1)
pthread_create(&t[i] ,NULL, func, (void*)i);
return 0;
}
When I run it, I get the following results (with a new line after each digit):
1st run : 0 0
2nd run : 1 0 2 2
3rd run : 0 1 1
Why does it print 4 digits when I only create 3 threads. And how can it print duplicates?
The code is compiled with gcc in Ubuntu.