Guest User

Untitled

a guest
Apr 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #define NUM_THREADS 5
  4.  
  5. void *PrintHello(void *threadid)
  6. {
  7. long tid;
  8. tid = (long)threadid;
  9. printf("Hello World! It's me, thread #%ld!n", tid);
  10. pthread_exit(NULL);
  11. }
  12.  
  13. int main (int argc, char *argv[])
  14. {
  15. pthread_t threads[NUM_THREADS];
  16. int rc;
  17. long t;
  18. for(t=0; t<NUM_THREADS; t++){
  19. printf("In main: creating thread %ldn", t);
  20. rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
  21. if (rc){
  22. printf("ERROR; return code from pthread_create() is %dn", rc);
  23. exit(-1);
  24. }
  25. }
  26. pthread_exit(NULL);
  27. }
  28.  
  29. corey@ubuntu:~/demo$ gcc -o term term.c
  30. term.c: In function ‘main’:
  31. term.c:23: warning: incompatible implicit declaration of built-in function ‘exit’
  32. /tmp/cc8BMzwx.o: In function `main':
  33. term.c:(.text+0x82): undefined reference to `pthread_create'
  34. collect2: ld returned 1 exit status
  35.  
  36. gcc -oterm -lpthread term.c
  37.  
  38. gcc -pthread -o term term.c
  39.  
  40. gcc -lpthread ...
  41.  
  42. gcc -lpthread -o term term.c
Add Comment
Please, Sign In to add comment