Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <fstream>
- #include <mutex>
- #include <thread>
- #include <future>
- #include <random>
- using namespace std;
- vector<string> file;
- string filePath = "data.txt";
- void readFile(int id)
- {
- // string empt = "empty";
- // cout << id << " : " << (file.empty() ? empt : file.back()) << "\n";
- ifstream infile;
- string line;
- string prevline = "empty";
- infile.open(filePath);
- while(getline(infile, line))
- prevline = line;
- cout << id << " : " << prevline << "\n";
- infile.close();
- }
- void writeFile(int id)
- {
- // file.push_back(to_string(id) + to_string(rand()%100));
- ofstream outfile;
- outfile.open(filePath, ios_base::app);
- outfile << to_string(id) + to_string(rand()%100) << "\n";
- outfile.close();
- }
- std::mutex resource1;
- std::mutex readCntMtx1;
- unsigned readersCount1 = 0;
- class Arbiter1
- {
- public:
- void waitReader()
- {
- readCntMtx1.lock();
- ++readersCount1;
- if (readersCount1 == 1)
- resource1.lock();
- readCntMtx1.unlock();
- }
- void signalReader()
- {
- readCntMtx1.lock();
- --readersCount1;
- if (readersCount1 == 0)
- resource1.unlock();
- readCntMtx1.unlock();
- }
- void waitWriter()
- {
- resource1.lock();
- }
- void signalWriter()
- {
- resource1.unlock();
- }
- };
- std::mutex resource2;
- std::mutex readCntMtx2;
- std::mutex writeCntMtx2;
- std::mutex readTryMtx2;
- unsigned readersCount2 = 0;
- unsigned writersCount2 = 0;
- class Arbiter2
- {
- public:
- void waitReader()
- {
- readTryMtx2.lock();
- readCntMtx2.lock();
- ++readersCount2;
- if (readersCount2 == 1)
- resource2.lock();
- readCntMtx2.unlock();
- readTryMtx2.unlock();
- }
- void signalReader()
- {
- readCntMtx2.lock();
- --readersCount2;
- if (readersCount2 == 0)
- resource2.unlock();
- readCntMtx2.unlock();
- }
- void waitWriter()
- {
- writeCntMtx2.lock();
- ++writersCount2;
- if (writersCount2 == 1)
- readTryMtx2.lock();
- writeCntMtx2.unlock();
- resource2.lock();
- }
- void signalWriter()
- {
- resource2.unlock();
- writeCntMtx2.lock();
- --writersCount2;
- if (writersCount2 == 0)
- readTryMtx2.unlock();
- writeCntMtx2.unlock();
- }
- };
- std::mutex resource3;
- std::mutex readCntMtx3;
- std::mutex queueMtx3;
- unsigned readersCount3 = 0;
- class Arbiter3
- {
- public:
- void waitReader()
- {
- queueMtx3.lock();
- readCntMtx3.lock();
- ++readersCount3;
- if (readersCount3 == 1)
- resource3.lock();
- queueMtx3.unlock();
- readCntMtx3.unlock();
- }
- void signalReader()
- {
- readCntMtx3.lock();
- --readersCount3;
- if (readersCount3 == 0)
- resource3.unlock();
- readCntMtx3.unlock();
- }
- void waitWriter()
- {
- queueMtx3.lock();
- resource3.lock();
- queueMtx3.unlock();
- }
- void signalWriter()
- {
- resource3.unlock();
- }
- };
- using Arbiter = Arbiter3;
- class Czytelnik
- {
- int id;
- Arbiter arbiter;
- public:
- Czytelnik(int i) : id(i) {}
- void read()
- {
- for (unsigned i = 0; i < 10; ++i)
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(rand()%100));
- arbiter.waitReader();
- readFile(id);
- arbiter.signalReader();
- }
- }
- };
- class Pisarz
- {
- int id;
- Arbiter arbiter;
- public:
- Pisarz(int i) : id(i) {}
- void write()
- {
- for (unsigned i = 0; i < 10; ++i)
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(20 + rand()%100));
- arbiter.waitWriter();
- writeFile(id);
- arbiter.signalWriter();
- }
- }
- };
- int main()
- {
- ofstream ofs;
- ofs.open(filePath, ofstream::out);
- ofs.close();
- Czytelnik cz1(11);
- Czytelnik cz2(22);
- std::thread reader1([&]{ cz1.read(); });
- std::thread reader2([&]{ cz2.read(); });
- Pisarz p1(10);
- Pisarz p2(20);
- std::thread writer1([&]{ p1.write(); });
- std::thread writer2([&]{ p2.write(); });
- reader1.join();
- reader2.join();
- writer1.join();
- writer2.join();
- // cout << "File:\n";
- // for (auto s : file)
- // cout << s << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement