#include<iostream>
#include<vector>
#include<queue>
#include<thread>
#include<random>
#include<mutex>
#include<condition_variable>
using namespace std;
int state = 0;
condition_variable cv;
mutex cv_mtx;
double read_sensor(int id) {
static thread_local std::mt19937 generator(id);
std::uniform_int_distribution<int> sleep_distribution(100, (id + 1) * 1000);
std::uniform_real_distribution<double> value_distribution(id, id + 0.99);
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_distribution(generator)));
return value_distribution(generator);
}
int main() {
vector<thread> threads_vec;
queue<pair<int,double>> sensorsData;
int numberOfSensors;
cin >> numberOfSensors;
unique_lock<mutex> lock(cv_mtx);
for (int i = 0; i < numberOfSensors; i++)
{
thread tmp([&sensorsData , i]() {
while (1)
{
unique_lock<mutex> lock(cv_mtx);
sensorsData.push(read_sensor(i));
cout << "sensor state" << state << endl;
cout.flush();
state++;
cv.notify_all();
}
});
threads_vec.push_back(move(tmp));
}
while (state != 5) {
cout << "New data!: ";
cv.wait(lock);
cout << sensorsData.front() << endl;
sensorsData.pop();
}
state = 0;
for (auto &t : threads_vec)
{
t.join();
}
system("pause");
return 0;
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}