🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

what is the advantage of using threadpool to handle connection in server?

Started by
1 comment, last by hplus0603 1 year, 11 months ago

I have been studying server design approach and I came upon a commonly used design pattern that a server uses threadpool to handle connections, this is not using ASIO/boost but native sockets, what im saying is when server listens and and accept connections, the new connections are passed to a threadpool instead of just simply creating a thread to handle that specific connections.

what is the advantage of said approach?

Advertisement

Each thread has overhead, in the form of stack space, and kernel resources. Each stack could be several megabytes, depending on the runtime!

If you want to do 100,000 connections on a 32-core machine, it's much more efficient to create 32 stacks/threads, than 100,000 stacks/threads.

Btw, ASIO and other wrappers typically do this under the hood, because it's more efficient.

The drawback is that you have to convert all your processing to be asynchronous; if you were to block waiting for a file read to complete, then that thread would be removed from the thread pool until the operation completed. (There are some special features in the Windows NT kernel that can adjust the number of threads in this case, if you want to read up on it …)

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement