fork(1) download
  1. #include <stdio.h>
  2. #include <pthread.h>
  3.  
  4. #define THREADS 4
  5.  
  6. void* print_message(void* arg) {
  7. int id = *(int*)arg;
  8. printf("Thread %d is running\n", id);
  9. return NULL;
  10. }
  11.  
  12. int main() {
  13. pthread_t threads[THREADS];
  14. int ids[THREADS];
  15.  
  16. for(int i = 0; i < THREADS; i++) {
  17. ids[i] = i + 1;
  18. pthread_create(&threads[i], NULL, print_message, &ids[i]);
  19. }
  20.  
  21. for(int i = 0; i < THREADS; i++) {
  22. pthread_join(threads[i], NULL);
  23. }
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Thread 4 is running
Thread 3 is running
Thread 2 is running
Thread 1 is running