fork download
  1. /*
  2.   *shial
  3.   *g++ -Wall -lpthread -o threads.exe thread.cpp -lncurses
  4.   */
  5.  
  6. #include <iostream>
  7. #include <deque>
  8. #include <vector>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include "ncurses.h"
  12.  
  13. #include <thread>
  14. #include <mutex>
  15. #include <condition_variable>
  16.  
  17. using namespace std;
  18.  
  19. int columns = 0;
  20. int rows = 0;
  21.  
  22. unsigned _licznik[30];
  23. bool _cout[30];
  24.  
  25. /*
  26.  * Kod Studenta.
  27.  * Student jest rekordem
  28.  * Posiadajacym int'a do którego wrzucamy 4 profesorów
  29.  */
  30. int UtworzStudenta()
  31. {
  32. int StudentRekord=0;
  33. for(int i=0;i<4;i++)
  34. StudentRekord|=(1<<(rand()%10)); // dodajemy losowych prowadzacych;
  35. return StudentRekord;
  36. }
  37.  
  38. std::mutex mutex_iostream;
  39.  
  40. void Legenda()
  41. {
  42. std::lock_guard<std::mutex> lk(mutex_iostream);
  43. move( 22, 0 );
  44. start_color();
  45. init_pair(7, COLOR_CYAN, COLOR_BLACK);
  46. attron(COLOR_PAIR(7));
  47. printw( "LEGENDA: " );
  48. attroff(COLOR_PAIR(7));
  49. init_pair(5, COLOR_GREEN, COLOR_BLACK);
  50. attron(COLOR_PAIR(5));
  51. printw( "WPIS, " );
  52. attroff(COLOR_PAIR(5));
  53. init_pair(1, COLOR_YELLOW, COLOR_BLACK);
  54. attron(COLOR_PAIR(1));
  55. printw( "QUEUE3, " );
  56. attroff(COLOR_PAIR(1));
  57. init_pair(2, COLOR_RED, COLOR_BLACK);
  58. attron(COLOR_PAIR(2));
  59. printw( "QUEUE2, " );
  60. attroff(COLOR_PAIR(2));
  61. init_pair(3, COLOR_WHITE, COLOR_BLACK);
  62. attron(COLOR_PAIR(3));
  63. printw( "QUEUE1, " );
  64. attroff(COLOR_PAIR(3));
  65. init_pair(4, COLOR_MAGENTA, COLOR_BLACK);
  66. attron(COLOR_PAIR(4));
  67. printw( "QUEUE0, " );
  68. attroff(COLOR_PAIR(4));
  69. refresh();
  70. }
  71.  
  72. void WypiszNaEkran(string tekst)
  73. {
  74. std::lock_guard<std::mutex> lk(mutex_iostream);
  75. move( 0, 0 );
  76. clrtoeol();
  77. printw( tekst.c_str() );
  78. refresh();
  79. }
  80.  
  81. void WypiszNaEkran(string tekst,int i, bool b)
  82. {
  83. std::lock_guard<std::mutex> lk(mutex_iostream);
  84. start_color();
  85. init_pair(5, COLOR_GREEN, COLOR_BLACK);
  86. attron(COLOR_PAIR(5));
  87. if(_licznik[i]>75)
  88. {
  89. _licznik[i]=0;
  90. move( 2*i, _licznik[i] );
  91. clrtoeol();
  92. }
  93. move( 2*i, _licznik[i] );
  94. _licznik[i]++;
  95. //clrtoeol();
  96. printw( "%c",232 );//tekst.c_str()
  97. attroff(COLOR_PAIR(5));
  98. refresh();
  99. }
  100. void WypiszNaEkran(string tekst,int i, int color)
  101. {
  102. std::lock_guard<std::mutex> lk(mutex_iostream);
  103. int farba=0;
  104. start_color();
  105. switch(color)
  106. {
  107. case 1: farba =1; init_pair(1, COLOR_YELLOW, COLOR_BLACK); break;
  108. case 2: farba =2; init_pair(2, COLOR_RED, COLOR_BLACK); break;
  109. case 3: farba =3; init_pair(3, COLOR_WHITE, COLOR_BLACK); break;
  110. case 4: farba =4; init_pair(4, COLOR_MAGENTA, COLOR_BLACK); break;
  111. default: farba =0; init_pair(0, COLOR_CYAN, COLOR_BLACK); break;
  112. }
  113. attron(COLOR_PAIR(farba));
  114. if(_licznik[i]>75)
  115. {
  116. _licznik[i]=0;
  117. move( 2*i, _licznik[i] );
  118. clrtoeol();
  119. }
  120. move( 2*i, _licznik[i] );
  121. _licznik[i]++;
  122. printw( "%c",232 );//tekst.c_str()
  123. attroff(COLOR_PAIR(farba));
  124. refresh();
  125. }
  126.  
  127. deque <int> kolejka0;
  128. deque <int> kolejka1;
  129. deque <int> kolejka2;
  130. deque <int> kolejka3;
  131.  
  132. struct Task
  133. {
  134. std::mutex mutex;
  135. } task0, task1, task2, task3;
  136.  
  137. //pthread_cond_t space_available = PTHREAD_COND_INITIALIZER;
  138.  
  139. void Profesor(int id);
  140. void Portier(int id);
  141.  
  142. std::vector<std::thread> profesorTab, portierTab;
  143.  
  144. int main()
  145. {
  146. srand (time(NULL));
  147. initscr();// ktora musimy zawsze użyc przed rozpoczeciem pracy z biblioteka New Curses.
  148. getmaxyx( stdscr, rows, columns ); //Pobieranie wartości okna do zmiennych
  149.  
  150. for(int i=0; i<30; i++)
  151. {
  152. _licznik[i]=0;
  153. _cout[i]=false;
  154. }
  155. //ustawiamy atrybuty na domyslna wartosc
  156.  
  157. Legenda();
  158.  
  159. for(int i=0; i<5; i++) portierTab.emplace_back(Portier, i+15);
  160. for(int i=0; i<10; i++) profesorTab.emplace_back(Profesor, i);
  161.  
  162. WypiszNaEkran("Dzialanie watkow");
  163.  
  164. //Rozpoczynamy dzialanie watkow
  165.  
  166. for(auto& th : portierTab)
  167. if (th.joinable()) th.join();
  168. for(auto& th : profesorTab)
  169. if (th.joinable()) th.join();
  170.  
  171. profesorTab.clear();
  172. WypiszNaEkran("Koniec Programu");
  173. endwin();// Konczy prace biblioteki New Curses.
  174. }
  175.  
  176. void foo1(int id)
  177. {
  178. std::lock_guard<std::mutex> lk(task3.mutex);
  179. // Sprawdzenie kolejki ze studentami, ktorzy maja juz trzy wpisy
  180. auto sz= kolejka3.size();
  181. if(!kolejka3.empty())
  182. {
  183. WypiszNaEkran("Profesor sprawdza kolejke3",id,1); //YELLOW
  184. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  185. for (unsigned j=0; j<sz; j++)
  186. {
  187. if(kolejka3[j]&(1<<id))
  188. {
  189. kolejka3[j]&=~(1<<id); // odznaczamy profesora
  190. kolejka3.erase(kolejka3.begin()+j);
  191. WypiszNaEkran("Profesor dodal swoj wpis(kolejka3)",id, true);
  192. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  193. }
  194. }
  195. }
  196. }
  197.  
  198. void foo2(int id)
  199. {
  200. std::lock_guard<std::mutex> lk(task2.mutex);
  201. // Sprawdzenie kolejki ze studentami, ktorzy maja dwa wpisy
  202. auto sz= kolejka2.size();
  203. if(!kolejka2.empty())
  204. {
  205. WypiszNaEkran("Profesor sprawdza kolejke2",id,2);//RED
  206. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  207. for (unsigned j=0; j<sz; j++)
  208. {
  209. if(kolejka2[j]&(1<<id))
  210. {
  211. kolejka2[j]&=~(1<<id); // odznaczamy profesora
  212. {
  213. std::lock_guard<std::mutex> lk(task3.mutex);
  214. kolejka3.push_back(kolejka2[j]);
  215. }
  216. kolejka2.erase(kolejka2.begin()+j);
  217. WypiszNaEkran("Profesor dodal swoj wpis(kolejka2)",id,true);
  218. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  219. }
  220. }
  221. }
  222. }
  223.  
  224. void foo3(int id)
  225. {
  226. std::lock_guard<std::mutex> lk(task1.mutex);
  227. // Sprawdzenie kolejki ze studentami, ktorzy maja jeden wpisy
  228. auto sz= kolejka1.size();
  229. if(!kolejka1.empty())
  230. {
  231. WypiszNaEkran("Profesor sprawdza kolejke1",id,3); // BLUE
  232. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  233. for (unsigned j=0; j<sz; j++)
  234. {
  235.  
  236. if(kolejka1[j]&(1<<id))
  237. {
  238. kolejka1[j]&=~(1<<id); // odznaczamy profesora
  239. {
  240. std::lock_guard<std::mutex> lk(task2.mutex);
  241. kolejka2.push_back(kolejka1[j]);
  242. }
  243. kolejka1.erase(kolejka1.begin()+j);
  244. WypiszNaEkran("Profesor dodal swoj wpis(kolejka2)",id,true);
  245. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  246. }
  247. }
  248. }
  249. }
  250.  
  251. void foo4(int id)
  252. {
  253. std::lock_guard<std::mutex> lk(task0.mutex);
  254. // Sprawdzenie kolejki ze studentami, ktorzy nie maja wpisow
  255. auto sz= kolejka0.size();
  256. if(!kolejka0.empty())
  257. {
  258. WypiszNaEkran("Profesor sprawdza kolejke0",id,4); // MAGENDA
  259. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  260. for (unsigned j=0; j<sz; j++)
  261. {
  262.  
  263. if(kolejka0[j]&(1<<id))
  264. {
  265. kolejka0[j]&=~(1<<id); // odznaczamy profesora
  266. {
  267. std::lock_guard<std::mutex> lk(task1.mutex);
  268. kolejka1.push_back(kolejka0[j]);
  269. }
  270. kolejka0.erase(kolejka0.begin()+j);
  271. WypiszNaEkran("Profesor dodal swoj wpis(kolejka0)",id,true);
  272. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  273. }
  274.  
  275. }
  276. }
  277. }
  278.  
  279. /*
  280.  * Kod Profesora.
  281.  *
  282.  * Profesor przeszukuje kolejki w kolejności 3,2,1
  283.  * w poszukiwaniu studenta który potrzebuje jego podpisu.
  284.  * Jeżeli znajduje to wyciąga go z tej kolejki, dodaje swój podpis(odznaczając się z rekordu)
  285.  * wrzuca do kolejnej (o ile nie wyciągnął z 3-ciej) wyżej
  286.  */
  287. void Profesor(int id)
  288. {
  289. while (1)
  290. {
  291. foo1(id); std::this_thread::sleep_for(std::chrono::seconds(1));
  292. foo2(id); std::this_thread::sleep_for(std::chrono::seconds(1));
  293. foo3(id); std::this_thread::sleep_for(std::chrono::seconds(1));
  294. foo4(id); std::this_thread::sleep_for(std::chrono::seconds(1));
  295. }
  296. }
  297.  
  298. /*
  299.  * Kod Portiera.
  300.  *
  301.  * Utrzymuje kolejke zerowa by bylo w niej przynajmnie 10 studentów
  302.  * Jezeli jest mniej niz 10 studentow dorabia 10 nowych
  303.  */
  304. void Portier(int id)
  305. {
  306. while(1)
  307. {
  308. {
  309. std::lock_guard<std::mutex> lk(task0.mutex);
  310. if(kolejka0.size()<10)
  311. {
  312. for(int i=0;i<10;i++)
  313. {
  314. kolejka0.push_back(UtworzStudenta());
  315. }
  316. }
  317. }
  318. std::this_thread::sleep_for(std::chrono::seconds(10));
  319. }
  320. }
  321.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:11:21: fatal error: ncurses.h: No such file or directory
compilation terminated.
stdout
Standard output is empty