Advertisement
Guest User

threads.c

a guest
Nov 12th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <stdint.h>
  6. #include <inttypes.h>
  7. #include <pthread.h>
  8.  
  9. void *func(void *arg) {
  10. sleep(4);
  11. printf("\n %" PRIiPTR, (intptr_t)arg);
  12. free(arg);
  13. pthread_exit(NULL);
  14. }
  15.  
  16. int main(){
  17.  
  18. int i = 1;
  19. int rc;
  20. int *p;
  21. int t = -1;
  22. pthread_t tld;
  23.  
  24. while(i) {
  25. p = (int*) malloc( sizeof(int));
  26. if(p == NULL ){
  27. perror("malloc failed: ");
  28. pthread_exit (NULL);
  29. }
  30. t=t+1;
  31. *p = t;
  32. rc = pthread_create( &tld, NULL, func, p);
  33. }
  34.  
  35. return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement