/
lyapdy
/
Object_oriented_programming_cpp_318
Обзор
Документация
Войти
/
lyapdy
/
Object_oriented_programming_cpp_318
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
lab7/task_4.cpp
320 строк
9 KB
danillyapunov
tasks 7 and 8 added
30 ноя 2025, 14:31
30 ноя 2025, 14:31
5a9b53a
Код
Авторство
О чём код?
#include "task_4.h" #include <iostream> #include <sstream> #include <chrono> #include <iomanip> MyMessage::MyMessage() { // �� 㬮�砭�� �����뢠�� �� ⨯� ᮮ�饭�� showEmergency = true; showError = true; showWarning = true; showSystemInfo = true; showServiceInfo = true; showDebug = true; } void MyMessage::setShowEmergency(bool show) { showEmergency = show; } void MyMessage::setShowError(bool show) { showError = show; } void MyMessage::setShowWarning(bool show) { showWarning = show; } void MyMessage::setShowSystemInfo(bool show) { showSystemInfo = show; } void MyMessage::setShowServiceInfo(bool show) { showServiceInfo = show; } void MyMessage::setShowDebug(bool show) { showDebug = show; } void MyMessage::emergency(const std::string& message, const std::string& function) { if (showEmergency) { std::string formatted = "Emergency: " + message; if (!function.empty()) { formatted += " [" + function + "]"; } messages.push_back(formatted); std::cout << formatted << '\n'; } } void MyMessage::error(const std::string& message, const std::string& function) { if (showError) { std::string formatted = "Error: " + message; if (!function.empty()) { formatted += " [" + function + "]"; } messages.push_back(formatted); std::cout << formatted << '\n'; } } void MyMessage::warning(const std::string& message, const std::string& function) { if (showWarning) { std::string formatted = "Warning: " + message; if (!function.empty()) { formatted += " [" + function + "]"; } messages.push_back(formatted); std::cout << formatted << '\n'; } } void MyMessage::systemInfo(const std::string& message, const std::string& function) { if (showSystemInfo) { std::string formatted = "SystemInfo: " + message; if (!function.empty()) { formatted += " [" + function + "]"; } messages.push_back(formatted); std::cout << formatted << '\n'; } } void MyMessage::serviceInfo(const std::string& message, const std::string& function) { if (showServiceInfo) { std::string formatted = "ServiceInfo: " + message; if (!function.empty()) { formatted += " [" + function + "]"; } messages.push_back(formatted); std::cout << formatted << '\n'; } } void MyMessage::debug(const std::string& message, const std::string& function) { if (showDebug) { std::string formatted = "Debug: " + message; if (!function.empty()) { formatted += " [" + function + "]"; } messages.push_back(formatted); std::cout << formatted << '\n'; } } std::vector<std::string> MyMessage::getAllMessages() const { return messages; } void MyMessage::clearMessages() { messages.clear(); } #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <vector> #include <random> #include <queue> #include <chrono> #include "task_4.h" class ThreadSafeQueue { private: std::queue<std::string> queue; std::mutex mtx; std::condition_variable cv; public: void push(const std::string& message) { std::lock_guard<std::mutex> lock(mtx); queue.push(message); cv.notify_one(); } std::string pop() { std::unique_lock<std::mutex> lock(mtx); cv.wait(lock, [this]() { return !queue.empty(); }); std::string message = queue.front(); queue.pop(); return message; } bool empty() { std::lock_guard<std::mutex> lock(mtx); return queue.empty(); } }; // �������� ��६���� ��� ᨭ����樨 ThreadSafeQueue errorQueue; std::mutex arrayMutex; std::vector<int> dataArray; bool stopThreads = false; MyMessage messageLogger; // �㭪�� ��� �����樨 ��砩���� � � ��������� int randomInt(int min, int max) { static std::random_device rd; static std::mt19937 gen(rd()); std::uniform_int_distribution<int> dis(min, max); return dis(gen); } // ��⮪ 1: ���������� ���ᨢ� ��砩�묨 ��� void fillArrayThread() { messageLogger.systemInfo("��⮪ ���������� ���ᨢ� ����饭", "fillArrayThread"); while (!stopThreads) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::lock_guard<std::mutex> lock(arrayMutex); // ������ �訡�� �뤥����� ����� (��砩��) if (randomInt(1, 100) < 5) { // 5% ����⭮��� �訡�� errorQueue.push("���������� �뤥���� ������ ��� ������"); messageLogger.error("�訡�� �뤥����� �����", "fillArrayThread"); continue; } // ������塞 ��砩��� � � ���ᨢ dataArray.push_back(randomInt(-200, 500)); messageLogger.debug("�������� ������� � ���ᨢ. ����騩 ࠧ���: " + std::to_string(dataArray.size()), "fillArrayThread"); } messageLogger.systemInfo("��⮪ ���������� ���ᨢ� �����襭", "fillArrayThread"); } // ��⮪ 2: �������� ���祭�� ���ᨢ� void filterArrayThread() { messageLogger.systemInfo("��⮪ 䨫���樨 ���ᨢ� ����饭", "filterArrayThread"); const int MIN_VALUE = 0; const int MAX_VALUE = 1024; while (!stopThreads) { std::this_thread::sleep_for(std::chrono::milliseconds(150)); std::lock_guard<std::mutex> lock(arrayMutex); if (dataArray.empty()) { continue; } // ������ �訡�� 䨫���樨 (��砩��) if (randomInt(1, 100) < 3) { // 3% ����⭮��� �訡�� errorQueue.push("�訡�� 䨫���樨: ������ ��������"); messageLogger.error("�訡�� 䨫���樨 ������", "filterArrayThread"); continue; } // �������� ���ᨢ� std::vector<int> filteredArray; for (int value : dataArray) { if (value >= MIN_VALUE && value <= MAX_VALUE) { filteredArray.push_back(value); } } dataArray = filteredArray; messageLogger.debug("���ᨢ ��䨫����. ���� ࠧ���: " + std::to_string(dataArray.size()), "filterArrayThread"); } messageLogger.systemInfo("��⮪ 䨫���樨 ���ᨢ� �����襭", "filterArrayThread"); } // ��⮪ 3: �뢮� ���祭�� ���ᨢ� void printArrayThread() { messageLogger.systemInfo("��⮪ �뢮�� ���ᨢ� ����饭", "printArrayThread"); while (!stopThreads) { std::this_thread::sleep_for(std::chrono::milliseconds(200)); std::lock_guard<std::mutex> lock(arrayMutex); if (dataArray.empty()) { messageLogger.warning("���ᨢ ����, ��祣� �뢮����", "printArrayThread"); continue; } // ������ �訡�� �뢮�� (��砩��) if (randomInt(1, 100) < 4) { // 4% ����⭮��� �訡�� errorQueue.push("�訡�� �뢮��: ���������� �뢥�� �����"); messageLogger.error("�訡�� �뢮�� ������", "printArrayThread"); continue; } // �뢮� ���ଠ樨 � ���ᨢ� std::string arrayInfo = "���ᨢ [" + std::to_string(dataArray.size()) + " ������⮢]: "; for (size_t i = 0; i < std::min(dataArray.size(), size_t(5)); ++i) { arrayInfo += std::to_string(dataArray[i]) + " "; } if (dataArray.size() > 5) { arrayInfo += "..."; } messageLogger.serviceInfo(arrayInfo, "printArrayThread"); } messageLogger.systemInfo("��⮪ �뢮�� ���ᨢ� �����襭", "printArrayThread"); } // ��⮪ ������: ��ࠡ�⪠ �訡�� �� ��।� void loggerThread() { messageLogger.systemInfo("��⮪ ������ ����饭", "loggerThread"); while (!stopThreads || !errorQueue.empty()) { try { std::string errorMessage = errorQueue.pop(); messageLogger.emergency("��ࠡ�⠭� �訡��: " + errorMessage, "loggerThread"); } catch (...) { break; } } messageLogger.systemInfo("��⮪ ������ �����襭", "loggerThread"); } int task_4() { std::cout << "=== ������� ���������� �������� � ������������� ===" << '\n'; // ����ன�� �⮡ࠦ����� ⨯�� ᮮ�饭�� messageLogger.setShowDebug(false); // �⪫�砥� �⫠���� ᮮ�饭�� messageLogger.setShowServiceInfo(true); // ����砥� �ࢨ�� ᮮ�饭�� // ����� ��⮪�� std::thread filler(fillArrayThread); std::thread filter(filterArrayThread); std::thread printer(printArrayThread); std::thread loggerT(loggerThread); // ���� ��⮪�� ��ࠡ���� 5 ᥪ㭤 std::this_thread::sleep_for(std::chrono::seconds(5)); // ��⠭���� ��⮪�� stopThreads = true; // �������� �����襭�� ��� ��⮪�� filler.join(); filter.join(); printer.join(); loggerT.join(); std::cout << "\n=== ��� ������ ��������� ===" << '\n'; std::cout << "=== ��������� ������� ===" << '\n'; // �뢮� ��� ᮡ࠭��� ᮮ�饭�� auto allMessages = messageLogger.getAllMessages(); for (const auto& msg : allMessages) { std::cout << msg << '\n'; } std::cout << "�ᥣ� ᮮ�饭��: " << allMessages.size() << '\n'; return 0; }