套接字服务器中每个客户端连接有新线程? [英] New thread per client connection in socket server?

查看:71
本文介绍了套接字服务器中每个客户端连接有新线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图每次优化与TCP套接字服务器的多个连接.

I am trying to optimize multiple connections per time to a TCP socket server.

每次接收到连接请求时,在侦听服务器中启动一个新线程是否被认为是一种好的做法,甚至是合理的?

Is it considered good practice, or even rational to initiate a new thread in the listening server every time I receive a connection request?

我应该什么时候开始担心基于此基础结构的服务器?直到不再有意义之前,我可以工作的最大后台线程数是多少?

At what time should I begin to worry about a server based on this infrastructure? What is the maximum no of background threads I can work, until it doesn't make any sense anymore?

平台是C#,框架是Mono,目标操作系统是CentOS,RAM是2.4G,服务器在云上,我希望每秒大约有200个连接请求.

Platform is C#, framework is Mono, target OS is CentOS, RAM is 2.4G, server is on the clouds, and I'm expecting about 200 connection requests per second.

推荐答案

否,每个连接不应有一个线程.相反,您应该使用异步方法(BeginAccept/EndAccept,BeginSend/EndSend等).这些将大大提高系统资源的利用率.

No, you shouldn't have one thread per connection. Instead, you should be using the asynchronous methods (BeginAccept/EndAccept, BeginSend/EndSend, etc). These will make much more efficient use of system resources.

尤其是,您创建的每个线程都会增加上下文切换,堆栈空间,缓存未命中等方面的开销.例如,Linux比Windows更擅长于管理这些内容,但这不应该成为让您自由创建任意数量的线程的借口;)

In particular, every thread you create adds overhead in terms of context switches, stack space, cache misses and so on. Linux is better at managing this stuff than Windows, for example, but that shouldn't be an excuse to give you free reign to create as many threads as you like ;)

这篇关于套接字服务器中每个客户端连接有新线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆