Search code examples
multithreadingclient-serverp2p

Peer-to-peer communication: is multithreading mandatory?


I have a system that has a central computer (server) and a group of small computers. These small computers can only communicate (by internet) with the central computer. No communication should be done between these small ones. It is like a master/slave concept with one master and several slaves but the communication is done from/to server to/from slave. Right now I decided to use a thread in the server for each small computer. If I have 10 small computers, 10 threads should be processing at the same time. Also noting that the server also have an application that can be accessed from browser. The information coming from the devices should be processed in the server and delivered to the application on the browser. Now, my doubt is, taking into account I have several small devices is it correct or mandatory to use 1 thread for each device? Or is there a tool that can manage all these connections? I'm using sockets by the way.

Thanks


Solution

  • No, it is not. What is mandatory is to preserve a unique context per client, assuming a TCP socket, it gonna be an open connection between both. You could rely upon your runtime and/or environment async framework and handle literally tens of thousands of concurrent connections on a single thread.

    However, the approach won't work for CPU-bound workflows; in other words, you have to make sure that IO-related losses (mostly, waiting for bytes to come in or get delivered and waiting for some external devices like a hard drive) dominate. Asynchronous way of doing things does not work for CPU-bound kinds of problems (for example, compression or hashing).

    Take a look into Erlang, for example. Or Golang. Just for inspiration.