English 中文(简体)
Optimal number of items to keep queued for the thread pool in .NET?
原标题:

I am building a background processing engine which supports discarding both to-be-processed and is-being-processed items. This is for usage in a winforms application that will require heavy processing of some input elements, so I m building a queue engine where I can enqueue workload items, and when they re processed, I get notified with the results.

The question is, this queue will almost always contain a lot of items to begin with, and I thought that instead of just dumping everything to the threadpool, I d place only the first N items into the threadpool, and keep backfilling when they are processed. The reason I want to do this is that once I dump them into the threadpool, they will be processed, and even if they re tagged as discard, they will still take up queue time.

With the backfill implementation I ve made, I can remove items from the queue if they become discarded, and only put them into the queue when it s their turn, so to speak.

So the question is, how would I go about calculating this number N, the number of items to place into and keep in the thread pool queue.

Issues I ve considered:

  • I might want to enqueue 2 * number of processors, which I see is a typical number of items, to ensure all processors are working
  • However, if the actual processing of some items is super-fast (which can happen), then the queue in the threadpool is exhausted before my own class can backfill with more work, so perhaps I d like a bigger number to avoid underutilizing the processors
  • Should I create some auto-adjust routine to calculate the optimal number based on the current time each item takes, so that if they are all super-fast, the number is much higher, and if processing takes a bit of time, it should stay low?

What do you think?

New: Ok, due to one of the answers, I ll explain a bit more. Every item put into the queue is keyed by something unique. If I dump another item into the queue with the same key as an existing item, that old item is considered "Discarded", and should be removed. If the item is being processed, a property on the workload item is set to true, a "IsDicarded" property, which the processing method is responsible for calling. If it detects a discarded item, it should quit early, returning no results.

Perhaps I should experiment a bit more, and try to just dump everything into the threadpool.

New question: Is there a limit to the number of items I can queue up? If not, then this would easily simplify my class a lot.

Note: When I say "lengthy processing", I mean in the order of 1-10 seconds. Is the threadpool even the best for this? I see notes all over the web about "the processing should be quick", but what "quick" is is never mentioned. Is quick in the order of milliseconds here?

最佳回答

Do you know Ami Bar s Smart Thread Pool?

Seems its implementation allows you to cancel an unprocessed item and dynamically increases threads as required, until a hard limit; I personally use 100 * Environment.ProcessorsCount

问题回答

Is it possible you could simplify the approach by modifying your items to first check they are still required before they do any work? This would skirt the problem of limiting the number in the pool, since you can simply add them all and when each item gets processed it will exit if no longer needed.

The number of operations that can be queued to the thread pool is limited only by available memory; however, the thread pool limits the number of threads that can be active in the process simultaneously. By default, the limit is 250 worker threads per CPU and 1,000 I/O completion threads.

You can control the maximum number of threads by using the GetMaxThreads and SetMaxThreads methods.

http://msdn.microsoft.com/en-us/library/0ka9477y.aspx





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签