I just started reading a pthreads tutorial and I was wondering why this:
Code:
#include <stdio.h>
#include <pthread.h>
void *th_go (void *num) {
printf("thread %d\n",*(int*)num);
pthread_exit(NULL);
}
int main() {
pthread_t th[3];
int i;
void *ptr=(void*)&i;
for (i=0; i<3; i++) {
pthread_create(&th[i],NULL,th_go,ptr);
}
return 0;
}
Does this:
Code:
[root~/C] ./a.out
thread 0
thread 1
thread 2
[root~/C] ./a.out
thread 1
thread 2
thread 2
[root~/C] ./a.out
thread 1
thread 1
[root~/C] ./a.out
thread 0
thread 1
thread 2
[root~/C] ./a.out
thread 1
thread 1
[root~/C] ./a.out
thread 0
thread 1
thread 2
[root~/C] ./a.out
thread 0
thread 2
thread 2