Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <random>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. typedef int semaphore;
  10. semaphore mutex = 1;
  11. semaphore resource = 1;
  12. int read_count = 0;
  13.  
  14. class Czytelnik{
  15. int id;
  16. public:
  17. Czytelnik(int i) : id(i) {}
  18. void read(){
  19. for(int i=0; i<10; i++){
  20. this_thread::sleep_for(chrono::milliseconds(rand() % 1000));
  21. cout<< id << endl;
  22. }
  23. }
  24. };
  25.  
  26. class Pisarz{
  27. int id;
  28. public:
  29. Pisarz(int i) : id(i) {}
  30. void write(){
  31. while(true){
  32. down(&resource);
  33. }
  34. cout<< id << endl;
  35. }
  36.  
  37. // lock_guard<semaphore> guard(mutex);
  38.  
  39.  
  40. };
  41.  
  42. class Arbiter{
  43.  
  44. };
  45.  
  46. int main() {
  47. Czytelnik c1(1);
  48. Czytelnik c2(2);
  49. thread t1 ([&]{c1.read(); });
  50. thread t2 ([&]{c2.read(); });
  51.  
  52. t1.join();
  53. t2.join();
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement