Had some typos in the previous post, correcting ...
---
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_ptr<boost::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)));
t1.detach();
// thread to do tasks as posted by dir_monitor thread and idle if no tasks exists
boost::thread t2(boost::bind(&boost::asio::io_service::run,
boost::ref(service2)));
t2.detach();
while (true) {
// do other stuff or idle, don't want main process to exit.
}