views:

537

answers:

3

Hi, i have little problem with boost::asio library. My app receive and process data asynchronously, it create threads and run io_service.run() on each of them.

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
    }
}

Time after time, my application receives message(across windows messaging system) from some supervisor application that checks is my program alive or not. If my application wont reply, it will be restarted. The tricky part here is to check, that threads is running and not deadlocked. I can post handler to io_service like this:

io.post( &reply_to_supervisor );

but this method affects only one thread. How can i check that all threads is running and not deadlocked?

A: 

I'm going to assume your io->run() does some sort of loop to wait for the asio to complete. I'm also going to assume you have a timeout on this asio operation. A dirty way to check is to have a status thread run and check that either the asio thread has timed out on waiting for the asio to complete or that an asio event has been posted. Either way, you would set some sort of variable or handle to know your thread is "alive" and looping. Your status thread would then check that each variable/handle and reset them after checking.

Mind you, I'm sure there are other ways but this is what came to mind as of right now... =)

PiNoYBoY82
+1  A: 

This strikes me as an instance of the Halting Problem, but since you appear to be on Windows, you might want to look at Just Software Solution's just::thread library. It is an implementation of the draft C++0x threading library, and has built-in deadlock detection in its own mutexes.

Ultimately though you are probably better off asking this question on the asio mailing list. The author of the library is quite helpful, and either he or some other hardcore asio user may be able to provide a better answer there.

Bklyn
Livelock would be just as bad, right?
MSalters
+1  A: 

I may be wrong, but would the use an 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

Vlagged