Search code examples
c++windowsclient-serveripcproducer-consumer

Single producer and multiple consumer using C++


I'm using C++ and have a simple client .exe that when handed a file name, it does process it and return success or error code. I want to create a Windows C++ .exe that does the following and was looking for sample code to do it:

  • Start 4 (or x) client .exe as separate process (for ex. using CreateProcess)
  • While the list of the files is not empty Send work to clients: Each client will process a sent file name and return either success
    or error code
  • Once the list of files to process is empty (or the producer .exe shutdown) close the
    4 clients (so they shutdown).

I did some research on this and found that pipes can be used to communicate between process. I found this sample app that does a communication between a server and client in c++: https://code.msdn.microsoft.com/windowsapps/CppNamedPipeServer-d1778534

The sample app does however sends request from client to server and get a response and I wanted to modify it or use a different sample app to do batch processing through having a common queue of work (or pipe that store this queue or batch of work) and send work to clients. I want to synchronize this work so as soon as client is done with one file, I'll send it another file to process.

Basically I want to create a sample application .exe that start multiple clients and send them work through inter-process communication. Any sample C++ code to do this is appreciated.

Thanks

Jeff Lacoste


Solution

  • You could have a look at boost. It has boost::interprocess where you can read about alot of ideas of what methods there are for IPC.

    I personally never use boost::interprocess as I'm a huge fan of boost::asio, and just like for your purposes, it has everything you need ( except creating a process ).

    And there is many many more to be found on google, and it is entirely opinion based what library to use or to directly use the native OS API, which is why I wonder this question is not closed yet.

    As for your request to give "code samples", those 2 links contain samples to everything you listed regarding IPC, and it's open source, so you can look how the libraries communicate with the native OS API.