mardi 26 janvier 2016

communication between Processes using NamedPipe and C++11 std::thread

i m using NamedPipe to communicate between 2 application in windows. Basically, a client and a server model: the client send a request to perform some operation to the server and get the result.

it worked fine until i added multi threading support for server: the server now wait for the client to connect and send his request. when it is ready the message is grabbed and it is given to a thread for processing it and the server(or the main thread) wait for another request.

here is the code :

ServerPipe pipe("Pipe");
OpFactory fact;

do {
if(pipe.waitForClient())
{
    RequestHandler reqH;
    BytesBuffer buf(pipe.readMessage());

    thread t(&RequestHandler::ProcessRequest , reqH, buf, &pipe, &fact);
    t.detach();
    // for t.join(); it works correctly but i don't want to wait for the 
    // std::thread to finish it has no sence fin this context.


}
++i;
}while(...some condition);
pipe.close();

for ServerPipe it a wrapper for WINAPI'NamedPipes, OpFactory is using do make the specified operation object.

the two programs block until i force one of the to shut down. the thread blocks on writeFile WINAPI and the other(client) on ReadFile(waiting for the result).

any ideas why this is happening?

Aucun commentaire:

Enregistrer un commentaire