Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Napisz program, w którym stworzysz 20 wątków wykonujących tę samą czynność. W momencie
- kiedy jeden z nich ją zakończy pozostałe powinny być anulowane w sposób asynchroniczny.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- void *threads_handler(void *p)
- {
- printf("Poczatek watku nr %lu\n", pthread_self());
- pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, (int *)NULL);
- for(int i = 0; i < 10; i++)
- usleep(10000);
- printf("Koniec watku nr %lu\n", pthread_self());
- return EXIT_SUCCESS;
- }
- int main(void)
- {
- pthread_t threads[20];
- for(int i = 0; i < 20; i++)
- pthread_create(&threads[i], NULL, threads_handler, (void *)&threads);
- pthread_join(threads[0], NULL);
- for(int i = 0; i < 20; i++)
- pthread_cancel(threads[i]);
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment