fork(1) download
  1. //main.c
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. void* routine(void* arg)
  6. {
  7. int id = (intptr_t) arg;
  8. printf("new thread %d\n", id);
  9. pthread_exit((void*)(intptr_t) id);
  10. }
  11. int main()
  12. {
  13. pthread_t t[2];
  14. int i;
  15. for(i=0; i<2; i++)
  16. {
  17. int ret = pthread_create (&t[i], NULL, &routine, (void *)(intptr_t) i);
  18. if(ret != 0) {
  19. printf("Error: pthread_create() failed\n");
  20. return -1;
  21. }
  22. }
  23. int id;
  24. /////////here
  25. for(i=0; i<2; i++)
  26. {
  27. printf("i: %d\n",i);
  28. pthread_join(t[i], (void **)&id);
  29. }
  30. /////////here
  31. pthread_exit(NULL);
  32. }
Success #stdin #stdout 0s 7536KB
stdin
Standard input is empty
stdout
i: 0
new thread 1
new thread 0
i: 1