开发者

Parallel ForEach, and Queues

开发者 https://www.devze.com 2023-03-16 10:05 出处:网络
Is it possible to have 开发者_开发技巧a parallel for each loop process the items in a queue such that it:

Is it possible to have 开发者_开发技巧a parallel for each loop process the items in a queue such that it:

  1. only removes items that are being processed
  2. Pauses until new items are added to the queue

EDIT: This is in regards to System.Threading.Tasks' Parallel.ForEach functionality


Subscribe to the queue using Reactive Extensions and execute each item in a new task. You won't have to block or wait for new item since it will be pushed on to your subscribe lambda and your execution/processing would be parallel.

http://rxwiki.wikidot.com/101samples


Use BlockingCollection something like this:

    var bc = new BlockingCollection<int>();
Task.Factory.StartNew(() =>
    {
        ParallelOptions options = new ParallelOptions
            {
                MaxDegreeOfParallelism = 30
            };
        Parallel.ForEach(bc.GetConsumingEnumerable(), options, i => { });
    });


I think it would be possible if the queue concrete implementation is synchronized, basically this is a consumer-producer scenario.


It sounds like you're talking about a classic producer/consumer situation. It always surprises me that there's not more direct support for this model built into .Net. The closest I've come across in .Net was at one time hidden away inside concurrency and coordination runtime, which was part of the Microsoft Robotics suite. Good luck finding it now.

But once you know what to search for, a quick Google check does provide a few other potential options.

0

精彩评论

暂无评论...
验证码 换一张
取 消