Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <mutex>
- #include <random>
- using namespace std;
- typedef int semaphore;
- semaphore mutex = 1;
- semaphore resource = 1;
- int read_count = 0;
- class Czytelnik{
- int id;
- public:
- Czytelnik(int i) : id(i) {}
- void read(){
- for(int i=0; i<10; i++){
- this_thread::sleep_for(chrono::milliseconds(rand() % 1000));
- cout<< id << endl;
- }
- }
- };
- class Pisarz{
- int id;
- public:
- Pisarz(int i) : id(i) {}
- void write(){
- while(true){
- down(&resource);
- }
- cout<< id << endl;
- }
- // lock_guard<semaphore> guard(mutex);
- };
- class Arbiter{
- };
- int main() {
- Czytelnik c1(1);
- Czytelnik c2(2);
- thread t1 ([&]{c1.read(); });
- thread t2 ([&]{c2.read(); });
- t1.join();
- t2.join();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement