Facebook
From Fabix, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 129
  1. #include<iostream>     
  2. #include<thread>
  3. #include<string>
  4. #include<ctime>
  5. #include<mutex>
  6. #include<queue>
  7. #include<fstream>
  8. #include<sstream>
  9. #include<random>
  10. using namespace std;
  11.  
  12. const int queueLenght = 4;
  13. const int botTimeVal = 1;
  14. const int topTimeVal = 8;
  15. string bufor[queueLenght];
  16.  
  17. int head = 0;
  18. int tail = 0;
  19.  
  20. int buforState(int head, int tail, int queueLenght) {
  21.         if (head == tail)
  22.                 return 0;
  23.         else if (head > tail || (tail > head && abs(head - tail) > 1))
  24.                 return -1;
  25.         else if (tail > head && abs(head - tail) > 1 || (head == queueLenght && tail == 0))
  26.                 return 1;
  27. }
  28.  
  29. int random_int(int min, int max) {
  30.         static std::mt19937 random_number_engine;
  31.         static std::mutex rng_mtx;
  32.         std::uniform_int_distribution<int> distribution(min, max);
  33.         rng_mtx.lock();
  34.         int retval = distribution(random_number_engine);
  35.         rng_mtx.unlock();
  36.         return retval;
  37. }
  38.  
  39. string addString() {
  40.         std::string temp;
  41.         temp.resize(10);
  42.         for (int i = 0; i < 10; i++) {
  43.                 temp[i] = random_int('a', 'z');
  44.         }
  45.         return temp;
  46. }
  47.  
  48. void producent(bool &on,const int queueLenght) {
  49.         while (on)
  50.         {
  51.                 int state = buforState(head, tail, queueLenght);
  52.                 string tmp = addString();
  53.                 if (state == 0 || state == -1)
  54.                 {
  55.                         bufor[head] = tmp;
  56.                         cerr << "Do kolejki dodano element o ID: " << tmp << endl;
  57.                         cerr << "HEAD : " << head << endl;
  58.                         if (head < queueLenght - 1)
  59.                                 head++;
  60.                         else
  61.                                 head = 0;
  62.                 }
  63.                 else if(state == 1) {
  64.                         cerr << "Obiekt o ID " << tmp << " nie trafil do kolejki" << endl;
  65.                         cerr << "HEAD : " << head << endl;
  66.                 }
  67.                 this_thread::sleep_for(chrono::seconds(random_int(botTimeVal, topTimeVal)));
  68.         }
  69. }
  70.  
  71. void consument(bool &on, const int queueLenght) {
  72.         ofstream file;
  73.         file.open("dane.txt");
  74.         while (on)
  75.         {
  76.                 int state = buforState(head, tail, queueLenght);
  77.                  if(state == 1 || state == -1){
  78.                         if (file.is_open()) {
  79.                                 file << bufor[tail] << endl;
  80.                                 cerr << "Zapisano element o ID : " << bufor[tail]<< endl;
  81.                                 cerr << "TAIL: " << tail << endl;
  82.                                 if (tail < queueLenght - 1)
  83.                                         tail++;
  84.                                 else
  85.                                         tail = 0;
  86.                         }
  87.                         else
  88.                                 cerr << "Nie udalo sie otworzyc pliku " << endl;
  89.                  }
  90.                 else if(state == 0){
  91.                         cerr << "PUSTO " << endl;
  92.                         cerr << "TAIL: " << tail << endl;
  93.                 }
  94.                 this_thread::sleep_for(chrono::seconds(random_int(botTimeVal, topTimeVal)));
  95.         }
  96.         file.close();
  97. }
  98.  
  99. int main() {
  100.         srand(time(NULL));
  101.         bool isOn = true;
  102.  
  103.         vector<thread> thread_vec;
  104.  
  105.         thread consument(consument, ref(isOn), queueLenght);
  106.         thread_vec.push_back(move(consument));
  107.         thread producent(producent, ref(isOn), queueLenght);
  108.         thread_vec.push_back(move(producent));
  109.  
  110.  
  111.         getchar();
  112.         isOn = false;
  113.  
  114.         for (auto &t : thread_vec) {
  115.                 t.join();
  116.         }
  117.  
  118.         system("pause");
  119.         return 0;
  120. }