0

My question :why not just pass &i as the last argument to pthread_create()? instead he create a array to hold the same thing....

#define THREAD_CT 2  /* bump this up a few numbers if you like */

void *print_stuff(void *ptr) {
  int i, id= * (int *) ptr;

  for (i= 0; i < 5; i++) {
    printf("Thread %d, loop %d.\n", id, i);
    sleep(rand() % 2);  /* sleep 0 or 1 seconds */
  }

  printf("Thread %d exiting.\n", id);

  return NULL;
}

int main(void) {
  pthread_t tids[THREAD_CT];
  int i, ids[THREAD_CT];

  for (i= 0; i < THREAD_CT; i++) {
    ids[i]= i;
    pthread_create(&tids[i], NULL, print_stuff, &ids[i]);
    printf("Main thread created thread %d (ID %ld).\n", i, tids[i]);
  }

  for (i= 0; i < THREAD_CT; i++) {
    pthread_join(tids[i], NULL);
    printf("Main thread reaped thread %d (ID %ld).\n", i, tids[i]);
  }

  return 0;
}
abligh
  • 24,573
  • 4
  • 47
  • 84
overloading
  • 145
  • 9

1 Answers1

4

why not just pass &i as the last argument to pthread_create()?

Because if you do that all threads will share the address i and there will be data race between threads.

The alternative is to cast the value like:

pthread_create(&tids[i], NULL, print_stuff, (void *)i);

But this integer to pointer conversion has implementation-defined behaviour. So the way you have it now is probably the best way

Also note that rand() is not thread-safe.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • I am very impressed from your answer @Bluemoon :-) – Punit Vara Dec 05 '15 at 19:45
  • One more question: why id= * (int *) ptr uses * (int *)??@BlueMoon – overloading Dec 05 '15 at 22:49
  • @overloading `ptr` is of type `void *` i.e. generic pointer. It can't be dereferenced directly because you don't know the underlying size of the actual type of pointer it's pointing to (it could a `char *`, `long *` or any other data pointer). So you need to cast it to the correct type before dereferencing. – P.P Dec 05 '15 at 22:51