BS_Praktikum4/sensor_network.cpp

54 lines
1.4 KiB
C++
Raw Normal View History

2025-06-03 00:50:08 +02:00
#include "sensor_network.h"
#include <iostream>
#include <random>
#include <chrono>
template <size_t N>
2025-06-03 01:25:44 +02:00
void SensorNetwork<N>::start(size_t sensors, size_t analysers) {
2025-06-03 00:50:08 +02:00
running = true;
2025-06-03 01:25:44 +02:00
// 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));
}
2025-06-03 00:50:08 +02:00
});
}
2025-06-03 01:25:44 +02:00
// 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";
}
2025-06-03 00:50:08 +02:00
});
}
2025-06-03 01:25:44 +02:00
// 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);
}
2025-06-03 00:50:08 +02:00
});
}
template <size_t N>
void SensorNetwork<N>::stop() {
running = false;
2025-06-03 01:25:44 +02:00
for (auto& t : threads) {
2025-06-03 00:50:08 +02:00
if (t.joinable()) t.join();
}
}
template class SensorNetwork<8>;
template class SensorNetwork<16>;
2025-06-03 01:25:44 +02:00
template class SensorNetwork<32>;