fork download
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4.  
  5. #define NUM_THREADS 3
  6.  
  7. void *hello_thread(void *arg)
  8. {
  9. printf("Thread %ld : Hello world\n", (long int)arg);
  10. return arg;
  11. }
  12.  
  13. int main(void) {
  14.  
  15. pthread_t tid[NUM_THREADS];
  16. long int i, status;
  17.  
  18. for(i=0 ; i<NUM_THREADS ; i++)
  19. {
  20. status = pthread_create(&tid[i], NULL, hello_thread, (void *)i);
  21. if(status !=0)
  22. {
  23. fprintf(stderr, "create thread %ld : %ld\n", i, status);
  24. exit(1);
  25. }
  26. }
  27. pthread_exit(NULL);
  28.  
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 5448KB
stdin
Standard input is empty
stdout
Thread 2 : Hello world
Thread 1 : Hello world
Thread 0 : Hello world