一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

IOCP Thread Pooling in C

时间:2022-07-02 11:11:38 编辑:袖梨 来源:一聚教程网

IOCP Thread Pooling in C#By William KennedyContinuum Technology CenterIntroduction
 
When building server based applications in C#, it is important to have the ability to create thread pools.  Thread pools allow our server to queue and perform work in the most efficient and scalable way possible.  Without thread pooling we are left with two options.  The first option is to perform all of the work on a single thread.  The second option is to spawn a thread every time some piece of work needs to be done.  For this article, work is defined as an event that requires the processing of code.  Work may or may not be associated with data, and it is our job to process all of the work our server receives in the most efficient and fastest way possible.
 
As a general rule, if you can accomplish all of the work required with a single thread, then only use a single thread.     Having multiple threads performing work at the same time does not necessarily mean our application is getting more work done, or getting work done faster.  This is true for many reasons. 
 
For example, if you spawn multiple threads which attempt to access the same resource bound to a synchronization object, like a monitor object, these threads will serialize and fall in line waiting for the resource to become available.  As each thread tries to access the resource, it has the potential to block, and wait for the thread that owns the resource to release the resource.  At that point, these waiting threads are put to sleep, and not getting any work done.  In fact, these waiting threads have caused more work for the operating system to perform.  Now the operating system must task another thread to perform work, and then determine which thread, waiting for the resource, may access the resource next, once it becomes available.  If the threads that need to perform work are sleeping, because they are waiting for the resource to become available, we have actually created a performance problem.  In this case it would be more efficient to queue up this work and have a single thread process the queue. 

热门栏目