/
dmitriy.grischenkov
/
calcstc
Обзор
Документация
Войти
/
dmitriy.grischenkov
/
calcstc
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/worker.cpp
102 строки
3 KB
Dmitriy Grischenkov
first load
01 авг 2026, 21:35
01 авг 2026, 21:35
166b1b5
Код
Авторство
О чём код?
#include <cmath> #include "worker.h" #include "libWrapper.h" ThreadSafeQueue<OneTask> Worker::m_reqQueue; ThreadSafeQueue<OneTask> Worker::m_resQueue; void Worker::heavyLoad(int delaySec) { volatile double x = 1.23456789; volatile double y = 9.87654321; const auto start = std::chrono::steady_clock::now(); const auto duration = std::chrono::seconds(delaySec); while (std::chrono::steady_clock::now() - start < duration) { x = std::sin(x) + std::cos(y); y = std::tan(x * y) - std::log(std::fabs(y) + 1.0); x *= 1.0001; y *= 0.9999; } } double Worker::doIt(int op, double a, double b) noexcept(false) { switch (op) { case CalcEnum::Plus: return a + b; case CalcEnum::Minus: return a - b; case CalcEnum::Multiply: return a * b; case CalcEnum::Divide: if (b == 0.0) throw std::logic_error("err"); return a / b; default: throw std::logic_error("err"); } } void Worker::start() { if (m_thread.joinable()) return; m_thread = std::thread(&Worker::run, this); } void Worker::stop() { shutdown(); if (m_thread.joinable()) m_thread.join(); } void Worker::shutdown() { m_stop = true; m_reqQueue.shutdown(); m_resQueue.shutdown(); } void Worker::run() { while (!m_stop) { OneTask task; if (m_reqQueue.pop(task)) { heavyLoad(task.m_delaySec); task.m_result = 0; auto opr = CalcEnum::Plus; bool err = false; QString errMes; for (size_t n = 0; n < task.m_operandVal.size(); ++n) { try { task.m_result = task.m_useExternal ? LibWrapper::call(opr, task.m_result, task.m_operandVal[n]) : doIt(opr, task.m_result, task.m_operandVal[n]); } catch (const std::logic_error& e) { errMes = e.what(); err = true; } catch (...) { errMes = "err"; err = true; } if (err) break; opr = task.m_operandOpr[n]; } if (err) task.m_resultStr.clear(); else task.m_resultStr.setNum(task.m_result); QString s1 = QString("<font color='#ffffaa'>%1</font>").arg(task.m_operandStr); QString s2 = QString("<font color='#aaddff'>%1</font>").arg(task.m_resultStr); QString s3 = QString("<font color='#ffaaaa'>%1</font>").arg(errMes); emit taskReady( s1 + s2 + s3 ); m_resQueue.push(std::move(task)); } else { std::this_thread::yield(); } } }