I got some students which I’m helping with a streaming server. They use synchronous sockets and one thread per client which isn’t really efficient. It’s better to use the asynchronous methods to let .Net utilize IOCP.
The thing is that I haven’t really spent any time thinking about when asynchronous sockets becomes more efficient when than the one thread/socket type of architecture. IIRC it’s most efficient two have two threads per core?
Can anyone shed some light in this? When do asynchronous sockets become more efficient? What is the optimal thread count per core?
The standard “one thread per one core” doesn’t apply here. The thread does a great deal of waiting for the slow connection and I/O to complete. Since a thread involved with sockets rarely does any real work in general, using asynchronous sockets very quickly become a win.
This changes if the async completion handler itself is doing a lot of waiting for I/O (maybe a disk or dbase), the threadpool scheduler can take a while to catch up with workers that don’t complete yet don’t burn cycles. Extra TP threads are added only twice a second.