0

The .Net Socket async API manages threads automatically when using the BeginXXX methods. For example, if I have 100 active connections sending and receiving TCP messages, will be used around 3 threads. And it makes me curious.

  • How the API makes this thread management?
  • How all flow of connections are divided among the threads to be processed?
  • How the manager prioritizes which connections/readings/writings must be processed first?

My questions may not have sense because I don't know how it works and what to ask specifically, so sorry. Basically I need to know how this whole process works in low level.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Gustavo Piucco
  • 467
  • 1
  • 7
  • 17
  • 1
    Check this [answer](http://stackoverflow.com/questions/5267535/how-does-an-asynchronous-socket-server-work) – Tinwor Jun 13 '14 at 22:33
  • Good question, but it seems to have been answered. (See @Tinwor 's comment). – Michael Hoffmann Jun 13 '14 at 22:39
  • @Tinwor, I disagree the linked question is a duplicate. It somewhat overlaps, but the answers do not explain how threads are allocated to serve the I/O completion and how they're returned to the pool, while that's what the OP is asking. – noseratio Jun 13 '14 at 23:41
  • 1
    http://msdn.microsoft.com/en-us/library/windows/desktop/aa365198%28v=vs.85%29.aspx – Hans Passant Jun 14 '14 at 08:22

1 Answers1

4

The .Net Socket async API manages threads automatically when using the BeginXXX methods.

This is not quite correct. APM Begin/End-style socket API do not manage threads at all. Rather, the completion AsyncCallback is called on a random thread, which is the thread where the asynchronous socket I/O operation has completed. Most likely, this is going to be an IOCP pool thread (I/O completion port thread), different from the thread on which you called the BeginXXX method. For more details, check Stephen Cleary's "There Is No Thread".

How the manager prioritizes which connections/readings/writings must be processed first?

The case when there's no IOCP threads available to handle the completion of the async I/O operation is called TheadPool starvation. It happens when all pool threads are busy executing some code (e.g., processing the received socket messages), or are blocked with a blocking call like WaitHandle.WaitOne(). In this case, the I/O completion routine is queued to ThreadPool to be executed when a thread becomes available, on FIFO basis.

You have an option to increase the size of ThreadPool with SetMinThreads/SetMaxThreads APIs, but doing so isn't always a good idea. The number of actual concurrent threads is anyway limited by the number of CPU/cores, so you'd rather want to finish any CPU-bound processing work as soon as possible and release the thread to go back to the pool.

noseratio
  • 59,932
  • 34
  • 208
  • 486