Search code examples
multithreadingvisual-c++timeoutparallel-processingboost-thread

General question about parallel threading in C++


I haven't used threading in my program before. But there is a problem I am having with this 3rd party application.

It is an offsite backup solution and it has a server and many clients. We have an admin console to manage all the clients and that is where there is a problem.

If one of the client side application gets stuck, or is running in a broken condition, the admin console waits forever to get a response and does not display anything.

$for(client= client1; client < last_client; client++){ if (getOServConnection(client, &socHandler)!=NULL) { .. } }

I want two solutions to this. I want to know if there is anyway, I can set a timeout for the function getOServConnection, so that I get a response within X seconds.

And, I want to know how to call this function in parallel for all clients, so that I get the response from all clients within X seconds.

the getOServConnection contains a WSAConnect call, and I don't want to use any options on the socket, since it is used by other modules and it will affect the application severely.


Solution

  • First.. If you move the call that hangs into a separate thread you can use the main thread for starting a timer an waiting for the timeout. If you are using Visual C++ and if you are in Win32 you can use the (rather old) MFC based timer. Once this timer expires it will launch a function call OnTimer. This timer does not affect your application's main thread as it works in a different system based thread.

    Second.. If you need to start any number of threads with that connection you should start thinking of a design pattern to use for that. You could use a fixed number of threads, and in that case you may want to use a object pool. Or if the number of threads is (relatively) limitless you may want to use a factory method