Heres another combination of many things I've been trying, but still cant get this to work. Unfortunately, this only works once, meaning it only receives one dir_monitor callback event, and if I cause another same event in a monitored directory, the f_handler() callback will no longer get called. I am able to post from thread1 to thread2 or from thread1 to the main process, but that also only works one time.
Any ideas?
---
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(service));
// thread to constantly monitor dirs
boost::thread t1 =
boost::thread(boost::bind(&boost::asio::io_service::run,
boost::ref(io_service)));
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(work_service)));
t2.detach();
while (true) {
// do other stuff or idle, don't want main process to exit.
}