The compiler will complain if you don't draw i on the void pointer:
pthread_create(&thread_tid[i], NULL, collector, (void*)i);
However, casting an integer to a pointer is not strictly safe:
ISO / IEC 9899: 201x 6.3.2.3 Indices
- An integer can be converted to any type of pointer. Except, as indicated earlier, the result is determined by the implementation, may not be aligned correctly, may not point to an object of a reference type, and may be a trap representation.
so you better pass a separate pointer to each thread.
Here's a complete working example that passes each thread a pointer to a single element in an array:
#include <pthread.h>
There is a nice introduction to pthreads here .
source share