/
lyapdy
/
Object_oriented_programming_cpp_318
Обзор
Документация
Войти
/
lyapdy
/
Object_oriented_programming_cpp_318
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
lab7/task_5.cpp
219 строк
6 KB
danillyapunov
tasks 7 and 8 added
30 ноя 2025, 14:31
30 ноя 2025, 14:31
5a9b53a
Код
Авторство
О чём код?
#include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <queue> #include <vector> #include <random> #include <chrono> #include <string> // ����� ��� ��⮪�������᭮� ��।� �訡�� class ErrQueue { private: std::queue<std::string> errors; std::mutex mtx; std::condition_variable cv; bool shutdown = false; public: // ���������� �訡�� � ��।� void push(const std::string& error) { { std::lock_guard<std::mutex> lock(mtx); errors.push(error); } cv.notify_one(); // �������塞 ������ � ����� �訡�� } // �����祭�� �訡�� �� ��।� (��������饥) std::string pop() { std::unique_lock<std::mutex> lock(mtx); // ����, ���� �� ����� �訡�� ��� �� �㤥� ����� �� �����襭�� cv.wait(lock, [this]() { return !errors.empty() || shutdown; }); if (shutdown && errors.empty()) { return ""; // ������ � �����襭�� } std::string error = errors.front(); errors.pop(); return error; } // ����� �� �����襭�� ࠡ��� void stop() { { std::lock_guard<std::mutex> lock(mtx); shutdown = true; } cv.notify_all(); // �㤨� �� ������騥 ��⮪� } // ��ઠ, ���� �� ��।� � ����襭� �� �����襭�� bool isStopped() { std::lock_guard<std::mutex> lock(mtx); return shutdown && errors.empty(); } }; // �������� ��६���� const int ARRAY_SIZE = 10; std::vector<int> data(ARRAY_SIZE); std::mutex data_mtx; ErrQueue errQueue; // �㭪�� ��� �����樨 ��砩���� � � ��������� int random_int(int min, int max) { static std::random_device rd; static std::mt19937 gen(rd()); std::uniform_int_distribution<int> dist(min, max); return dist(gen); } // ���� ࠡ�稩 ��⮪ - ���������� ���ᨢ� void worker_fill() { try { while (true) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); { std::lock_guard<std::mutex> lock(data_mtx); for (int i = 0; i < ARRAY_SIZE; ++i) { data[i] = random_int(-200, 1200); } } // ���砩��� �訡�� � ����⭮���� 5% if (random_int(1, 100) <= 5) { errQueue.push("Worker Fill: �訡�� ���������� ���ᨢ� (��砩��� �訡��)"); } // ���� ��室� ��� ��������樨 if (random_int(1, 500) <= 2) { errQueue.push("Worker Fill: �����襭�� ࠡ���"); break; } } } catch (const std::exception& e) { errQueue.push(std::string("Worker Fill: �᪫�祭�� - ") + e.what()); } } // ��ன ࠡ�稩 ��⮪ - 䨫����� ���ᨢ� void worker_filter(int min_val, int max_val) { try { while (true) { std::this_thread::sleep_for(std::chrono::milliseconds(150)); { std::lock_guard<std::mutex> lock(data_mtx); for (int i = 0; i < ARRAY_SIZE; ++i) { if (data[i] < min_val || data[i] > max_val) { data[i] = 0; // �����塞 ��������� ���祭�� �� 0 } } } // ���砩��� �訡�� � ����⭮���� 5% if (random_int(1, 100) <= 5) { errQueue.push("Worker Filter: �訡�� 䨫���樨 ���ᨢ� (��砩��� �訡��)"); } // ���� ��室� ��� ��������樨 if (random_int(1, 500) <= 2) { errQueue.push("Worker Filter: �����襭�� ࠡ���"); break; } } } catch (const std::exception& e) { errQueue.push(std::string("Worker Filter: �᪫�祭�� - ") + e.what()); } } // ��⨩ ࠡ�稩 ��⮪ - �뢮� ���ᨢ� void worker_print() { try { while (true) { std::this_thread::sleep_for(std::chrono::milliseconds(200)); { std::lock_guard<std::mutex> lock(data_mtx); std::cout << "���ᨢ: ["; for (int i = 0; i < ARRAY_SIZE; ++i) { std::cout << data[i]; if (i < ARRAY_SIZE - 1) std::cout << ", "; } std::cout << "]" << std::endl; } // ���砩��� �訡�� � ����⭮���� 5% if (random_int(1, 100) <= 5) { errQueue.push("Worker Print: �訡�� �뢮�� ���ᨢ� (��砩��� �訡��)"); } // ���� ��室� ��� ��������樨 if (random_int(1, 500) <= 2) { errQueue.push("Worker Print: �����襭�� ࠡ���"); break; } } } catch (const std::exception& e) { errQueue.push(std::string("Worker Print: �᪫�祭�� - ") + e.what()); } } // ��⮪ ������ - ��ࠡ�⪠ �訡�� void logger() { std::cout << "������ ����饭 � ������� �訡��..." << std::endl; int error_count = 0; while (true) { std::string error = errQueue.pop(); if (error.empty() && errQueue.isStopped()) { break; // ��室 �� ᨣ���� �����襭�� } if (!error.empty()) { ++error_count; std::cout << "������ [�訡�� #" << error_count << "]: " << error << std::endl; } // ������ �६��� ��ࠡ�⪨ �訡�� std::this_thread::sleep_for(std::chrono::milliseconds(50)); } std::cout << "������ �����訫 ࠡ���. ��ࠡ�⠭� �訡��: " << error_count << std::endl; } int task_5() { std::cout << "����� ��⥬� ࠡ��� ��⮪�� � ������..." << std::endl; // ����� ������ std::thread logger_thread(logger); // ����� ࠡ��� ��⮪�� std::thread fill_thread(worker_fill); std::thread filter_thread(worker_filter, 0, 1024); // �������� 䨫���樨: 0-1024 std::thread print_thread(worker_print); // ���� �����襭�� ࠡ��� ��⮪�� fill_thread.join(); filter_thread.join(); print_thread.join(); // ����������㥬 ������� � �����襭�� errQueue.stop(); logger_thread.join(); std::cout << "�� ��⮪� �����訫� ࠡ���." << std::endl; return 0; }