BS_Praktikum4/sensor_network.cpp
2025-06-03 01:25:44 +02:00

53 lines
1.4 KiB
C++

#include "sensor_network.h"
#include <iostream>
#include <random>
#include <chrono>
template <size_t N>
void SensorNetwork<N>::start(size_t sensors, size_t analysers) {
running = true;
// Sensor threads
for (size_t i = 0; i < sensors; ++i) {
threads.emplace_back([this] {
std::mt19937 gen(std::random_device{}());
std::uniform_int_distribution<> dist(0, 100);
while (running) {
std::this_thread::sleep_for(std::chrono::milliseconds(100 + gen() % 400));
buffer.push(dist(gen));
}
});
}
// Analyser threads
for (size_t i = 0; i < analysers; ++i) {
threads.emplace_back([this] {
while (running) {
int data = buffer.pop();
int model_val = model.read();
std::cout << "Data: " << data << " Model: " << model_val << "\n";
}
});
}
// Controller thread
threads.emplace_back([this] {
std::mt19937 gen(std::random_device{}());
while (running) {
std::this_thread::sleep_for(std::chrono::milliseconds(500 + gen() % 1500));
model.write(gen() % 100);
}
});
}
template <size_t N>
void SensorNetwork<N>::stop() {
running = false;
for (auto& t : threads) {
if (t.joinable()) t.join();
}
}
template class SensorNetwork<8>;
template class SensorNetwork<16>;
template class SensorNetwork<32>;