Question

J'ai petit problème avec la bibliothèque boost :: asio. Mon application recevoir et traiter les données de manière asynchrone, il créer des threads et exécuter io_service.run () sur chacun d'eux.

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

Heure après heure, mon application reçoit un message (à travers le système de messagerie Windows) depuis une application de supervision qui vérifie est mon programme vivant ou non. Si ma demande réponse coutume, il sera redémarré. La partie la plus délicate est de vérifier ici, que les fils est en cours d'exécution et non dans l'impasse. Je ne peux pas poster gestionnaire à io_service comme ceci:

io.post( &reply_to_supervisor );

mais cette méthode ne touche qu'un seul fil. Comment puis-je vérifier que tous les threads est en cours d'exécution et non dans l'impasse?

Était-ce utile?

La solution

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

Autres conseils

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.

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... =)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top