/
lyapdy
/
Object_oriented_programming_cpp_318
Обзор
Документация
Войти
/
lyapdy
/
Object_oriented_programming_cpp_318
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
lab7/task_1.cpp
134 строки
4 KB
danillyapunov
tasks 7 and 8 added
30 ноя 2025, 14:31
30 ноя 2025, 14:31
5a9b53a
Код
Авторство
О чём код?
#include "task_1.h" #include <iostream> #include <thread> #include <vector> #include <mutex> #include <condition_variable> #include <atomic> class FibonacciCalculator { private: unsigned int n; unsigned long result; std::mutex mtx; std::condition_variable cv; std::atomic<unsigned int> current_iteration; std::atomic<bool> calculation_done; unsigned int num_threads; public: FibonacciCalculator(unsigned int num) : n(num), result(0), current_iteration(2), calculation_done(false) { // ����砥� ������⢮ 䨧��᪨� 拉� num_threads = std::thread::hardware_concurrency(); if (num_threads == 0) { num_threads = 4; // fallback } std::cout << "������⢮ ��⮪��: " << num_threads << std::endl; } void calculate() { // ���塞 �᪫�祭�� ��� n = 9 if (n == 9) { throw std::runtime_error("����� ������� 9-�� � ��������!"); } if (n <= 1) { result = n; return; } unsigned long prev = 0; unsigned long curr = 1; // ������ ��砨 if (n == 0) { result = 0; return; } if (n == 1) { result = 1; return; } // ������� � ����᪠�� ��⮪� std::vector<std::thread> threads; for (unsigned int i = 0; i < num_threads; ++i) { threads.emplace_back(&FibonacciCalculator::worker_thread, this, i, std::ref(prev), std::ref(curr)); } // ���� �����襭�� ��� ��⮪�� for (auto& t : threads) { t.join(); } result = curr; } void worker_thread(unsigned int thread_id, unsigned long& prev, unsigned long& curr) { std::cout << "��⮪ " << thread_id << " ����饭 " << std::endl; while (true) { std::unique_lock<std::mutex> lock(mtx); // ���塞, �㦭� �� �த������ ������ if (current_iteration > n) { std::cout << "��⮪ " << thread_id << " �����蠥� ࠡ��� (������ �����襭�)" << std::endl; calculation_done = true; cv.notify_all(); return; } // ������ ����� ��� �⮣� ��⮪� unsigned int iter = current_iteration; current_iteration++; std::cout << "��⮪ " << thread_id << " ������ ����� " << iter << std::endl; // ������ ���饣� � �������� unsigned long next = prev + curr; prev = curr; curr = next; std::cout << "��⮪ " << thread_id << " �����訫 ����� " << iter << ", १����: " << curr << std::endl; // �������, ���� ��㣨� ��⮪� 㢨��� ���������� ����� cv.notify_all(); // � ���⨣�� �㦭�� ���樨, ��室�� if (iter >= n) { calculation_done = true; cv.notify_all(); std::cout << "��⮪ " << thread_id << " �����蠥� ࠡ��� (���⨣��� ����� " << n << ")" << std::endl; return; } } } unsigned long getResult() const { return result; } }; int task_1() { std::vector<unsigned int> test_cases = {8, 9, 10}; for (unsigned int n : test_cases) { std::cout << "\n=== ���� ��� n = " << n << " ===" << std::endl; try { FibonacciCalculator calculator(n); calculator.calculate(); std::cout << "F(" << n << ") = " << calculator.getResult() << std::endl; } catch (const std::exception& e) { std::cout << "�᪫�祭�� �������: " << e.what() << std::endl; } std::cout << "=== �����襭�� ��� ��� n = " << n << " ===\n" << std::endl; } std::cout << "�㭪�� main() �����襭�!" << std::endl; return 0; }