boost::asio::io_service service1; boost::asio::io_service service2;
// dir_monitor callback running in thread 1 void f_handler(const boost::system::error_code &ec, const boost::asio::dir_monitor_event &ev) { TaskObj tsk = new TaskObj(...); // detect event, create task and post functor to thread 2 which handles // tasks
service2.post(boost::bind(&TaskObj::do_it, tsk)); }
int main(...) { boost::asio::dir_monitor dm(service1);
for (/*directory list*/) { dm.add_directory(dir); }
dm.async_monitor(f_handler);
// setup work to prevent exit and before running io_service shared_ptrboost::asio::io_service::work work(new boost::asio::io_service::work(service2));
// thread to constantly monitor dirs boost::thread t1 = boost::thread(boost::bind(&boost::asio::io_service::run, boost::ref(service1)));
First of all, I don't know how dir_monitor works (it's not a part of an official Boost.Asio). But if its interface semantic is consistent with other Asio interfaces, than async_monitor should asynchronously monitor 1 event. So, after async_monitor gets completed, service1 runs out of work and its run() loop exits (not that you didn't give it any "work" object).