C# - 当使用标准的线程,线程池和TPL在高活动服务器 [英] C# - When to use standard threads, ThreadPool, and TPL in a high-activity server

查看:273
本文介绍了C# - 当使用标准的线程,线程池和TPL在高活动服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读了很多关于线程最近,因为我希望开发一个高性能,可扩展的TCP服务器能够处理高达10,000-20,000客户,每个客户端,其中一直被用双向连通到服务器命令为基础的系统。服务器将接收的命令,并且执行任一单个(或多个)任务根据该命令。我的问题是如何适当地利用.NET线程构建体适用于各种情况下,执行可能需要一分钟之间至数小时,这取决于工作正在执行的任务。

I've been reading a lot about threading lately as I am looking to develop a high-performance, scalable TCP server capable of handling up to 10,000-20,000 clients, each client of which is consistently communicating bidirectionally to the server with a command-based system. The server will receive a command, and execute either a single (or many) tasks as per the command. My question is how to appropriately make use of the .NET threading constructs for a variety of situations, executing tasks that could take between one minute to several hours, depending on the work being performed.

什么是最困惑我的是,我到处看,我看到类似使用手动创建的线程(或自定义线程池)来处理长时间运行的事实任务,并使用TPL的短期任务,或者需要并行处理任务。究竟的的长时间运行的任务吗?那是5秒,60秒,一小时?

What's confusing me the most is the fact that everywhere I read, I see something like "use a manually created Thread (or custom thread pool) to handle 'long-running' tasks, and use TPL for short-lived tasks, or tasks that require parallel processing." What exactly is a long-running task? Is that 5 seconds, 60 seconds, an hour?

通过什么时间,我应该使用每个创建线程的这三种方法:

With what time frame should I be using each of these three methods of creating threads:


  • 手动创建的线程

  • 的.NET ThreadPool类

  • TPL

我想到另外一个问题是如下 - 说我的服务器事实上确实有20,000个客户端连接,其中每发送1命令(可以转化为一个或多个任务)每秒。即使有强大的硬件,是不存在的,我可以推过高的工作量成任何线程池/工作项目排队我也有,从而最终产生一个OutOfMemoryException机会后,队列慢慢填充到最大?

Another issue I've contemplated is as follows--say my server does in fact have 20,000 clients connected, each of which sends 1 command (which could translate to one or many tasks) per second. Even with powerful hardware, isn't there a chance that I could be pushing too high of a workload into whatever thread pool / work item queue I have, thereby eventually generating an OutOfMemoryException after the queue slowly fills to the maximum?

任何有识之士将不胜感激。

Any insight would be greatly appreciated.

推荐答案

实际上,对于该方案的的所有是次要的;你应该看看的第一件事就是ASYC-IO,又名 .BeginRead(...)等;这使您可以通过等待IO完成端口,以尽量减少线程的数量 - 更有效

Actually, for that scenario all of those are secondary; the first thing you should look at is asyc-IO, aka .BeginRead(...) etc; this allows you to minimise the number of threads by waiting on IO completion ports - much more efficient.

一旦你有一个完整的消息,的在那个尺度我就把消息到自定义线程池/同步队列。我想有一个控制数量的常规的线程队列来处理每个项目(非池中的线程或IOCP)服务的。

Once you have a complete message, at that scale I would throw the message into a custom thread-pool/synchronized-queue. I would have a controlled number of regular threads (not pool threads or IOCP) servicing that queue to process each item.

碰巧我在做此刻类似(下标);防止内存炸响,我已经封顶工作队列;如果它得到充分(即工人跟不上),那么你可能会阻止IOCP一小会,也许超时最终,告诉客户太忙的IOCP层。

As it happens I'm doing something similar (lower scale) at the moment; to prevent memory exploding, I have capped the work queue; if it gets full (i.e. the workers can't keep up) then you might block IOCP for a small while, perhaps with a timeout eventually that tells the client "too busy" at the IOCP layer.

这篇关于C# - 当使用标准的线程,线程池和TPL在高活动服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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