Tobiahao

S01_WATKI_04

Dec 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. /*
  2. Napisz program, w którym stworzysz jeden wątek łączny i jeden wątek rozdzielny oraz zademon-
  3. strujesz różnicę w działaniu tych wątków.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <pthread.h>
  9.  
  10. void *first_thread_function(void *p)
  11. {
  12.     printf("Watek laczny nr: %lu\n", pthread_self());
  13.     return EXIT_SUCCESS;
  14. }
  15.  
  16. void *second_thread_function(void *p)
  17. {
  18.     printf("Watek rozdzielny nr: %lu\n", pthread_self());
  19.     return EXIT_SUCCESS;
  20. }
  21.  
  22. int main(void)
  23. {
  24.     pthread_t first_thread, second_thread;
  25.     pthread_attr_t first_attr, second_attr;
  26.  
  27.     pthread_attr_init(&first_attr);
  28.     pthread_attr_init(&second_attr);
  29.  
  30.     pthread_attr_setdetachstate(&first_attr, PTHREAD_CREATE_JOINABLE);
  31.     pthread_attr_setdetachstate(&second_attr, PTHREAD_CREATE_DETACHED);
  32.  
  33.     pthread_create(&first_thread, &first_attr, first_thread_function, (void *)0);
  34.     pthread_create(&second_thread, &second_attr, second_thread_function, (void *)0);
  35.    
  36.     pthread_join(first_thread, NULL);
  37.     pthread_join(second_thread, NULL);
  38.  
  39.     pthread_attr_destroy(&first_attr);
  40.     pthread_attr_destroy(&second_attr);
  41.  
  42.     return EXIT_SUCCESS;
  43. }
Add Comment
Please, Sign In to add comment