#include <pthread.h>
#include <stdio.h>

#define NUM_THREADS 5

void *thread_routine(void *data)
{
   // you can't just access 'data' directly - it is changing down below!
   //  'num' makes a local copy
   int num = (int)data;

   printf("I am thread %d\n", num);
   pthread_exit(NULL);
}

int main(int argc, int* argv[])
{
   int i;
   pthread_t t[NUM_THREADS];

   for (i = 0; i < NUM_THREADS; i++)
   {  pthread_create(&t[i], NULL, thread_routine, (void *)i);
   }
   pthread_exit(NULL);
}
Topic revision: r1 - 27 Sep 2008 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback