c++ - How to call a function when a work item is finished in Boost.Asio? -
i implement command queue handles incoming commands concurrently thread pool (so queue grows temporarily when threads working). post callback callers when command worker started , finished. implementation based on this example asio website.
is there way hook these events , signal somehow? avoid command functors knowing callbacks (since call callbacks inside command functors).
pseudocode illustrate (initialization , error handling omitted brevity):
class commandqueue { public: void handle_command(cmdid id, int param) { io_service.post(boost::bind(&(dispatch_map[id]), param)); // pseudocode: // when 1 of worker threads start item, want call callback_site.cmd_started(id, param); // when command functor returns , thread finished callback_site.cmd_finished(id, param); } private: boost::asio::io_service io_service; asio::io_service::work work; std::map<cmdid, commandhandler> dispatch_map; // commandhandler functor taking int parameter callbacksite callback_site; };
is there way without having command functors depend on callbacksite?
so want build in happens when 1 of run()
commands starts process command, , on return.
personally, wrapping function call:
class commandqueue { public: void handle_command(cmdid id, int param) { io_service.post(boost::bind(&commandqueue::dispatchcommand, this,id,param)); } private: boost::asio::io_service io_service; asio::io_service::work work; std::map<cmdid, commandhandler> dispatch_map; // commandhandler functor taking int parameter callbacksite callback_site; void dispatchcommand(cmdid id, int param) { // when 1 of worker threads start item, want call callback_site.cmd_started(id, param); dispatch_map[id](param); // when command functor returns , thread finished callback_site.cmd_finished(id, param); } };
this pattern use when want handle exceptions in dispatched commands. can post different events instead of running them inline.
Comments
Post a Comment