#ifndef IOCONTEXT_H #define IOCONTEXT_H #include "Singleton.h" #include #include #include #include class IoContext { public: friend class Amass::Singleton; std::shared_ptr ioContext() const { return m_ioContext; } void run(bool asynchronous = true) ; void stop(); ~IoContext(); /** * @brief * * @tparam Executor boost::asio::io_context、boost::asio::strand(通过 boost::asio::make_strand(io_context); 得到) */ template static void postDelayed(Executor &ioContext, Func &&task, const std::chrono::duration &duration) { auto timer = std::make_shared(ioContext, duration); timer->async_wait([timer, task = std::forward(task)](const boost::system::error_code & /*e*/) mutable { boost::asio::post(timer->get_executor(), std::move(task)); }); } template static void postDelayed(Executor &ioContext, Func &&task, int milliseconds) { return postDelayed(ioContext, std::forward(task), std::chrono::milliseconds(milliseconds)); } protected: IoContext(int concurrency); void runIoContext(); private: int m_concurrency = 1; std::shared_ptr m_ioContext; std::unique_ptr> m_guarder; std::vector m_threads; bool m_asynchronous=false; }; #endif // IOCONTEXT_H