Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. pthread_mutex_t mutex;
  4. int counter = 0;
  5.  
  6. void odejmowanie() {
  7. pthread_mutex_lock(&mutex);
  8. for(int i = 0; i < 100000; i++) {
  9. counter--;
  10. printf("%d ", counter);
  11. }
  12. pthread_mutex_unlock(&mutex);
  13. }
  14.  
  15. void dodawanie() {
  16. pthread_mutex_lock(&mutex);
  17. for (int i = 0; i < 100000; i++) {
  18. counter++;
  19. printf("%d ", counter);
  20. }
  21. pthread_mutex_unlock(&mutex);
  22. }
  23.  
  24. void main() {
  25. pthread_t dod, odej;
  26. pthread_mutex_init(&mutex, 0);
  27. int result;
  28. pthread_create(&dod, NULL, dodawanie, NULL);
  29. pthread_create(&odej, NULL, odejmowanie, NULL);
  30. pthread_join(dod, &result);
  31. pthread_join(odej, &result);
  32. pthread_detach(&dod);
  33. pthread_detach(&odej);
  34. printf("%d ", counter);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement