Tobiahao

S01_WATKI_08

Dec 19th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None | 0 0
  1. /*
  2. Napisz program, w którym stworzysz 20 wątków wykonujących tę samą czynność. W momencie
  3. kiedy jeden z nich ją zakończy pozostałe powinny być anulowane w sposób asynchroniczny.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <pthread.h>
  9. #include <unistd.h>
  10.  
  11. void *threads_handler(void *p)
  12. {
  13.     printf("Poczatek watku nr %lu\n", pthread_self());
  14.     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, (int *)NULL);
  15.  
  16.     for(int i = 0; i < 10; i++)
  17.         usleep(10000);
  18.  
  19.     printf("Koniec watku nr %lu\n", pthread_self());
  20.     return EXIT_SUCCESS;
  21. }
  22.  
  23. int main(void)
  24. {
  25.     pthread_t threads[20];
  26.     for(int i = 0; i < 20; i++)
  27.         pthread_create(&threads[i], NULL, threads_handler, (void *)&threads);
  28.  
  29.     pthread_join(threads[0], NULL);
  30.  
  31.     for(int i = 0; i < 20; i++)
  32.         pthread_cancel(threads[i]);
  33.  
  34.     return EXIT_SUCCESS;
  35. }
Add Comment
Please, Sign In to add comment