Facebook
From Abrupt Prairie Dog, 3 Years ago, written in Plain Text.
">

A PHP Error was encountered

Severity: Notice

Message: Trying to access array offset on value of type bool

Filename: view/view.php

Line Number: 33

from

A PHP Error was encountered

Severity: Notice

Message: Trying to access array offset on value of type bool

Filename: view/view.php

Line Number: 33

- view diff
Embed
Download Paste or View Raw
Hits: 166
  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.  
  10. using namespace std;
  11.  
  12.  
  13. string addString() {
  14.         std::string temp;
  15.         temp.resize(10);
  16.         for (int i = 0; i < 10; i++) {
  17.                 temp[i] = rand() % ('z' - 'a' + 1) + 'a';
  18.         }
  19.         return temp;
  20. }
  21.  
  22. void producent(bool &on , queue<string> &vec, const int queueLenght) {
  23.         while (on)
  24.         {
  25.                 string tmp = addString();
  26.                 if (vec.size() < queueLenght){
  27.                         vec.push(tmp);
  28.                         cerr << tmp << endl;
  29.                 }
  30.  
  31.                 else
  32.                 cerr << "Obiekt o ID " << tmp << " nie trafil do kolejki" << endl;
  33.  
  34.  
  35.                 this_thread::sleep_for(chrono::seconds(1));
  36.         }
  37. }
  38.  
  39. void consument(bool &on, queue<string> &vec, const int queueLenght, ofstream &file) {
  40.         while (on)
  41.         {
  42.                 if (!vec.empty()) {
  43.                         if (file.is_open()){
  44.                                 file << vec.front() << endl;
  45.                                 cerr << "Zapisano element o ID : " << vec.front() << endl;
  46.                                 vec.pop();
  47.                                 this_thread::sleep_for(chrono::seconds(1));
  48.                         }
  49.                 }
  50.         }
  51.         file.close();
  52. }
  53.  
  54. int main() {
  55.         srand(time(NULL));
  56.         bool isOn = true;
  57.         const int queueLenght = 10;
  58.         int noCons = 2;
  59.         int noProd = 2;
  60.  
  61.         queue<string> bufor;
  62.         vector<thread> thread_vec_c;
  63.         vector<thread> thread_vec_p;
  64.  
  65.  
  66.         for (int i = 0; i < noCons; i++) {
  67.                 ofstream file;
  68.                 thread::id ID = this_thread::get_id();
  69.                 stringstream ss;
  70.                 ss << ID;
  71.                 string mystring = ss.str();
  72.                 string mystringtxt = mystring + ".txt";
  73.                 file.open(mystringtxt);
  74.                 thread tmp(consument , ref(isOn), ref(bufor), queueLenght, ref(file));
  75.                 thread_vec_c.push_back(move(tmp));
  76.         }
  77.  
  78.         for (int i = 0; i < noProd; i++) {
  79.                 thread tmp(producent, ref(isOn), ref(bufor), queueLenght);
  80.                 thread_vec_p.push_back(move(tmp));
  81.         }
  82.  
  83.        
  84.         getchar();
  85.         isOn = false;
  86.  
  87.         for (auto &t : thread_vec_p) {
  88.                 t.join();
  89.         }
  90.  
  91.         for (auto &t : thread_vec_c) {
  92.                 t.join();
  93.         }
  94.         system("pause");
  95.         return 0;
  96. }