8

有人知道如何在 PLINQ 中编写返回 ParallelQuery 的扩展函数吗?

更具体地说,我有以下问题:我想在需要引擎的 PLINQ 查询中执行转换,该引擎的创建成本很高并且不能同时访问。

我可以执行以下操作:

var result = source.AsParallel ().Select ( (i) => { var e = new Engine (); return e.Process(i); } )

在这里,每个项目创建一次引擎,这太昂贵了。

我希望每个线程创建一次引擎。

使用聚合,我可以接近我想要的东西

// helper class: engine to use plus list of results obtained in thread so far
class EngineAndResults {
   public Engine engine = null;
   public IEnumerable<ResultType> results;
}

var result = source.AsParallel ().Aggregate (

   // done once per block of items (=thread),
   // returning an empty list, but a new engine
   () => new EngineAndList () {
       engine = new Engine (),
       results = Enumerable.Empty<ResultType> ()
   },

   // we process a new item and put it to the thread-local list,
   // preserving the engine for further use
   (engineAndResults, item) => new EngineAndResults () {
       engine = engineAndResults.engine,
       results = Enumerable.Concat (
           engineAndResults.results,
           new ResultType [] { engineAndResults.engine.Process (item) }
       )
   },

   // tell linq how to aggregate across threads
   (engineAndResults1, engineAndResults2) => new EngineAndResults () {
       engine = engineAndResults1.engine,
       results = Enumerable.Concat (engineAndResults1.results, engineAndResults2.results)
   },

   // after all aggregations, how do we come to the result?
   engineAndResults => engineAndResults.results
);

如您所见,我滥用累加器为每个线程携带一个引擎。这里的问题是 PLINQ 最终将结果聚合到单个 IEnumerable 中,这会导致线程同步。如果我想在之后附加另一个 PLINQ 扩展,这不是很好。

我会很感激像

   var result = source.AsParallel ()
                  .SelectWithThreadwiseInitWhichIAmLookingFor (
                       () => new Engine (),
                       (engine, item) => engine.Process (item)
              )

有谁知道如何实现这一目标?

4

1 回答 1

5

你可以ThreadLocal<T>用来做这个。就像是:

var engine = new ThreadLocal<Engine>(() => new Engine());
var result = source.AsParallel()
                   .Select(item => engine.Value.Process(item));
于 2012-06-22T13:33:52.877 回答