#ifndef EBDC071F_8F58_4C5D_ACB9_7C534616BEC5 #define EBDC071F_8F58_4C5D_ACB9_7C534616BEC5 /* ******************************************************************************* * INCLUDE ******************************************************************************* */ #include #include #include #include #include #include #include #include namespace Util { /* ******************************************************************************* * CLASS DECLARE ******************************************************************************* */ class Task { public: Task(const std::string& name = "Anonymous"); public: void run(std::function& isCancelled)> func); template static void run2(Func&& func, Args&&... args) { run2WithError(std::forward(func), nullptr, std::forward(args)...); } template static void run2WithError(Func&& func, std::function onError, Args&&... args) { auto boundFunc = std::bind(std::forward(func), std::forward(args)...); std::thread( [boundFunc, onError]() mutable { try { boundFunc(); } catch (const std::exception& e) { if (onError) { onError(std::current_exception()); } else { std::cerr << "Util::Task::run2 exception: " << e.what() << std::endl; } } catch (...) { if (onError) { onError(std::current_exception()); } else { std::cerr << "Util::Task::run2 exception!" << std::endl; } } }) .detach(); } void cancel(); bool isRunning(); const std::string& name() const; private: const std::string _name; std::atomic _isCancelled; Poco::FastMutex _lock; Poco::SharedPtr _pThread; }; inline const std::string& Task::name() const { return _name; } } #endif