How do I detect a dead end with the Asio library?

I have few problems with boost :: asio library. My application receives and processes data asynchronously, creates threads, and runs io_service.run () for each one.

boost::asio::io_service io;
boost::thread_group thread_pool;
...
int cpu_cnt = get_cpu_count();
for (int i = 0; i < cpu_cnt; ++i)
{
    thread_pool.create_thread( boost::bind(&run_service, &io) );
}

void run_service(boost::asio::io_service* io)
{
    try
    {
        io->run();//make fun
    }
    catch(const std::exception& e)
    { //process error
    }
    catch(...)
    { //process error
    }
}

      

From time to time, my app receives a message (via Windows messaging) from some supervisor app that checks if my program is alive or not. If my application doesn't respond, it will restart. The tricky part here is to check that the threads are running and not blocked. I can send a handler to io_service like this:

io.post( &reply_to_supervisor );

      

but this method only affects one thread. How can I check that all threads are running and not blocked?

+1


source to share


3 answers


I may be wrong, but could using io_service per thread solve your problem?



Another idea: post cpu_cnt

times reply_to_supervisor

calls that use a little sleep()

- not nice, but should work

+2


source


This looks like an example Stopping problem , but since you appear to be on Windows, you can look at Just Software Solution only :: thread . It is an implementation of the C ++ 0x thread design library and has deadlock detection built in in its own mutexes.



Ultimately though, you're probably better off asking this question on the asio mailing list . The library author is very helpful and either he or some other hardcore asio user can provide a better answer.

+1


source


I'm going to assume that your io-> run () is doing some kind of loop to wait for asio to finish. I also assume you have a timeout in this asio operation. The dirty way of checking is to start the state thread and check that either the asio thread has been disconnected while waiting for the asio to complete, or an asio event has been dispatched. In any case, you must set some kind of variable or handle to know that your stream is live and circular. Then your status thread checks that each variable / handle and reset after checking.

Keep in mind, I'm sure there are other ways, but this is what came to mind now ... =)

0


source







All Articles