I have an application that is sending messages over a socket from different threads. I create my own wrapper for the socket that enables me to extend functionality.

Example - on new SocketConnection() it returns only one instance based on IP and port. So if some other thread tries to create instance of IP and port 1000 every time it gets the same instance if it already exists. (Single instance for one device.)

  • Auto connect
  • State machine for connection etc.

Every unique instance has a send message queue (from higher app) and has a Task/Thread that is running in a loop and checking if something is in the queue to be sent, then processes socket.send (if queue is not empty). When the send is successful or fails then respond using a callback to the process that added the message to the queue.

I'm not sure if this is a good solution to have for each unique instance a Task/Thread running in a loop. Imagine I have 100 endpoint devices (machines) and my app is from different threads sending data to these devices.

If I have 100 devices to communicate with, than I have 100 unique instances with 100 threads/Tasks running in loop and processing queues.

App generates to one of these devices (for example 50 requests to send from different upper threads) to destination device with IP port 70. My unique class for this IP, adds this to messages queue and thread-loop of this instance, processes these messages one by one. Upper level just gets callback if socket.send() was OK or failed.

Is this good way how to process many connection? The same way I'm receiving responses from devices. (The Device is a server.)

What I know now is that using create new thread is faster than using Task for loops in each instance. Is it better to use timer interrupt? Or what's the best way to process incoming communications?

Are there any patterns how to do that?

1 Answer 1


While spawning a thread for handling each connection is a simple way to handle the concurrency, and is probably going to work for under 10.000 or so connections, it is not the most efficient use of server resources when you have a higher number of mostly idle connections, as spawning, destroying and swapping threads causes system overhead.

Take a look at this question which has a very good comparative of possible implementations. Your options under MSW are somewhat more limited, and a thread per socket might still be the best possible under this system.

  • I have crossplatform app.. May 13, 2019 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.