Tobiahao

S01_WATKI_02

Dec 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. /*
  2. Stwórz dwa wątki w programie. Każdemu z nich przekaż przez parametr funkcji dwie liczby. Pierw-
  3. szy wątek niech policzy sumę tych liczb, drugi różnicę. Obie wartości należy wypisać na ekran
  4. w wątkach.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <pthread.h>
  10.  
  11. struct number {
  12.     int x, y;
  13. };
  14.  
  15. void *add_thread_function(void *arg)
  16. {
  17.     struct number *num = arg;
  18.     printf("Dodawanie: %d\n", num->x+num->y);
  19.     return EXIT_SUCCESS;
  20. }
  21.  
  22. void *subtract_thread_function(void *arg)
  23. {
  24.     struct number *num = arg;
  25.     printf("Odejmowanie: %d\n", num->x-num->y);
  26.     return EXIT_SUCCESS;
  27. }
  28.  
  29. int main(void)
  30. {
  31.     pthread_t add_thread, subtract_thread;
  32.     struct number *num;
  33.     num->x = 5;
  34.     num->y = 3;
  35.  
  36.     pthread_create(&add_thread, NULL, add_thread_function, (void *)num);
  37.     pthread_create(&subtract_thread, NULL, subtract_thread_function, (void *)num);
  38.  
  39.     pthread_join(add_thread, NULL);
  40.     pthread_join(subtract_thread, NULL);
  41.  
  42.     return EXIT_SUCCESS;
  43. }
Add Comment
Please, Sign In to add comment