Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <fstream>
  3. #include <mutex>
  4. #include <thread>
  5. #include <future>
  6. #include <random>
  7.  
  8. using namespace std;
  9.  
  10. vector<string> file;
  11. string filePath = "data.txt";
  12.  
  13. void readFile(int id)
  14. {
  15. // string empt = "empty";
  16. // cout << id << " : " << (file.empty() ? empt : file.back()) << "\n";
  17. ifstream infile;
  18. string line;
  19. string prevline = "empty";
  20. infile.open(filePath);
  21. while(getline(infile, line))
  22. prevline = line;
  23. cout << id << " : " << prevline << "\n";
  24. infile.close();
  25. }
  26. void writeFile(int id)
  27. {
  28. // file.push_back(to_string(id) + to_string(rand()%100));
  29. ofstream outfile;
  30. outfile.open(filePath, ios_base::app);
  31. outfile << to_string(id) + to_string(rand()%100) << "\n";
  32. outfile.close();
  33. }
  34.  
  35. std::mutex resource1;
  36. std::mutex readCntMtx1;
  37. unsigned readersCount1 = 0;
  38.  
  39. class Arbiter1
  40. {
  41. public:
  42. void waitReader()
  43. {
  44. readCntMtx1.lock();
  45. ++readersCount1;
  46. if (readersCount1 == 1)
  47. resource1.lock();
  48. readCntMtx1.unlock();
  49. }
  50. void signalReader()
  51. {
  52. readCntMtx1.lock();
  53. --readersCount1;
  54. if (readersCount1 == 0)
  55. resource1.unlock();
  56. readCntMtx1.unlock();
  57. }
  58.  
  59. void waitWriter()
  60. {
  61. resource1.lock();
  62. }
  63. void signalWriter()
  64. {
  65. resource1.unlock();
  66. }
  67. };
  68.  
  69.  
  70. std::mutex resource2;
  71. std::mutex readCntMtx2;
  72. std::mutex writeCntMtx2;
  73. std::mutex readTryMtx2;
  74.  
  75. unsigned readersCount2 = 0;
  76. unsigned writersCount2 = 0;
  77.  
  78. class Arbiter2
  79. {
  80. public:
  81. void waitReader()
  82. {
  83. readTryMtx2.lock();
  84. readCntMtx2.lock();
  85.  
  86. ++readersCount2;
  87. if (readersCount2 == 1)
  88. resource2.lock();
  89.  
  90. readCntMtx2.unlock();
  91. readTryMtx2.unlock();
  92. }
  93. void signalReader()
  94. {
  95. readCntMtx2.lock();
  96. --readersCount2;
  97. if (readersCount2 == 0)
  98. resource2.unlock();
  99. readCntMtx2.unlock();
  100. }
  101.  
  102. void waitWriter()
  103. {
  104. writeCntMtx2.lock();
  105. ++writersCount2;
  106. if (writersCount2 == 1)
  107. readTryMtx2.lock();
  108. writeCntMtx2.unlock();
  109.  
  110. resource2.lock();
  111. }
  112. void signalWriter()
  113. {
  114. resource2.unlock();
  115.  
  116. writeCntMtx2.lock();
  117. --writersCount2;
  118. if (writersCount2 == 0)
  119. readTryMtx2.unlock();
  120. writeCntMtx2.unlock();
  121. }
  122. };
  123.  
  124.  
  125. std::mutex resource3;
  126. std::mutex readCntMtx3;
  127. std::mutex queueMtx3;
  128.  
  129. unsigned readersCount3 = 0;
  130.  
  131. class Arbiter3
  132. {
  133. public:
  134. void waitReader()
  135. {
  136. queueMtx3.lock();
  137. readCntMtx3.lock();
  138.  
  139. ++readersCount3;
  140. if (readersCount3 == 1)
  141. resource3.lock();
  142.  
  143. queueMtx3.unlock();
  144. readCntMtx3.unlock();
  145. }
  146. void signalReader()
  147. {
  148. readCntMtx3.lock();
  149. --readersCount3;
  150. if (readersCount3 == 0)
  151. resource3.unlock();
  152. readCntMtx3.unlock();
  153. }
  154.  
  155. void waitWriter()
  156. {
  157. queueMtx3.lock();
  158. resource3.lock();
  159. queueMtx3.unlock();
  160. }
  161. void signalWriter()
  162. {
  163. resource3.unlock();
  164. }
  165. };
  166.  
  167. using Arbiter = Arbiter3;
  168.  
  169. class Czytelnik
  170. {
  171. int id;
  172. Arbiter arbiter;
  173.  
  174. public:
  175. Czytelnik(int i) : id(i) {}
  176. void read()
  177. {
  178. for (unsigned i = 0; i < 10; ++i)
  179. {
  180. std::this_thread::sleep_for(std::chrono::milliseconds(rand()%100));
  181. arbiter.waitReader();
  182. readFile(id);
  183. arbiter.signalReader();
  184. }
  185. }
  186. };
  187.  
  188. class Pisarz
  189. {
  190. int id;
  191. Arbiter arbiter;
  192.  
  193. public:
  194. Pisarz(int i) : id(i) {}
  195. void write()
  196. {
  197. for (unsigned i = 0; i < 10; ++i)
  198. {
  199. std::this_thread::sleep_for(std::chrono::milliseconds(20 + rand()%100));
  200. arbiter.waitWriter();
  201. writeFile(id);
  202. arbiter.signalWriter();
  203. }
  204. }
  205. };
  206.  
  207. int main()
  208. {
  209. ofstream ofs;
  210. ofs.open(filePath, ofstream::out);
  211. ofs.close();
  212.  
  213. Czytelnik cz1(11);
  214. Czytelnik cz2(22);
  215. std::thread reader1([&]{ cz1.read(); });
  216. std::thread reader2([&]{ cz2.read(); });
  217.  
  218. Pisarz p1(10);
  219. Pisarz p2(20);
  220. std::thread writer1([&]{ p1.write(); });
  221. std::thread writer2([&]{ p2.write(); });
  222.  
  223. reader1.join();
  224. reader2.join();
  225. writer1.join();
  226. writer2.join();
  227.  
  228. // cout << "File:\n";
  229. // for (auto s : file)
  230. // cout << s << "\n";
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement