Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. void read_lock()
  2. {
  3. //Locks the mutex
  4. pthread_mutex_lock(&lock);
  5.  
  6. while (writerWaiting)
  7. {
  8. pthread_cond_wait(&cond, &lock);
  9. }
  10.  
  11. ++readersWaiting;
  12. }
  13.  
  14. void read_unlock()
  15. {
  16. --readersWaiting;
  17.  
  18. //IMPL1
  19. while (readersWaiting > 0)
  20. {
  21. pthread_cond_wait(&cond, &lock);
  22. }
  23.  
  24. pthread_cond_signal(&cond);
  25. pthread_mutex_unlock(&lock);
  26. }
  27.  
  28. void write_lock()
  29. {
  30. pthread_mutex_lock(&lock);
  31.  
  32. while (writerWaiting)
  33. {
  34. pthread_cond_wait(&cond, &lock);
  35. }
  36. }
  37.  
  38. void write_unlock()
  39. {
  40. writerWaiting = true;
  41.  
  42. while (readersWaiting > 0)
  43. {
  44. pthread_cond_wait(&cond, &lock);
  45. }
  46.  
  47. writerWaiting = false;
  48.  
  49. pthread_cond_broadcast(&cond);
  50. pthread_mutex_unlock(&lock);
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement