I'm trying to design and implement a program that is going to "solve" the classic producer-consumer problem. I'll also be using the publish/subscribe pattern and there's only one producer.
The first thing that came to my mind was to implement each thing as a separate thread, but then I realized that this is inefficient and there probably are smarter ways to do it.
I need to:
- accept tcp/ip connections from clients
- get the data using a named pipe from another process (named pipe can change to anything else here)
- as soon as I get the data decide which clients should get it and send it there
- be able to parse client commands at any time (eg. client wants to change the subscription details)
- also depending on implementation details I might want to clear the collection that holds incoming data(if only I need to have one) and kick inactive clients
My idea now is:
- create a thread that is going to accept connections from the clients and add each client to a list
- create a thread/pool of threads that are going to execute a strand(
boost::strand
) that is going to consist of reading the data from the source, deciding which client should get data and asynchronously writing the data to the clients.
I do not know if
- this is valid approach
- this is efficient approach
- how to add reading commands from the client(s) ?
- how to check client inactivity
I would be grateful for any comments and proposals on how to properly implement something like this.