Parallel.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. // Parallel.cs
  2. //
  3. // Copyright (c) 2008 Jérémie "Garuma" Laval
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. #if NET_4_0
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Collections.Concurrent;
  28. using System.Threading;
  29. namespace System.Threading.Tasks
  30. {
  31. public static class Parallel
  32. {
  33. internal static int GetBestWorkerNumber ()
  34. {
  35. return GetBestWorkerNumber (TaskScheduler.Current);
  36. }
  37. internal static int GetBestWorkerNumber (TaskScheduler scheduler)
  38. {
  39. return scheduler.MaximumConcurrencyLevel;
  40. }
  41. static int GetBestWorkerNumber (int from, int to, ParallelOptions options, out int step)
  42. {
  43. int num = Math.Min (GetBestWorkerNumber (),
  44. options != null && options.MaxDegreeOfParallelism != -1 ? options.MaxDegreeOfParallelism : int.MaxValue);
  45. // Integer range that each task process
  46. if ((step = (to - from) / num) < 5) {
  47. step = 5;
  48. num = (to - from) / 5;
  49. if (num < 1)
  50. num = 1;
  51. }
  52. return num;
  53. }
  54. static void HandleExceptions (IEnumerable<Task> tasks)
  55. {
  56. HandleExceptions (tasks, null);
  57. }
  58. static void HandleExceptions (IEnumerable<Task> tasks, ParallelLoopState.ExternalInfos infos)
  59. {
  60. List<Exception> exs = new List<Exception> ();
  61. foreach (Task t in tasks) {
  62. if (t.Exception != null)
  63. exs.Add (t.Exception);
  64. }
  65. if (exs.Count > 0) {
  66. if (infos != null)
  67. infos.IsExceptional = true;
  68. throw new AggregateException (exs);
  69. }
  70. }
  71. static void InitTasks (Task[] tasks, int count, Action action, ParallelOptions options)
  72. {
  73. TaskCreationOptions creation = TaskCreationOptions.LongRunning;
  74. for (int i = 0; i < count; i++) {
  75. if (options == null)
  76. tasks [i] = Task.Factory.StartNew (action, creation);
  77. else
  78. tasks [i] = Task.Factory.StartNew (action, options.CancellationToken, creation, options.TaskScheduler);
  79. }
  80. }
  81. #region For
  82. public static ParallelLoopResult For (int from, int to, Action<int> action)
  83. {
  84. return For (from, to, ParallelOptions.Default, action);
  85. }
  86. public static ParallelLoopResult For (int from, int to, Action<int, ParallelLoopState> action)
  87. {
  88. return For (from, to, ParallelOptions.Default, action);
  89. }
  90. public static ParallelLoopResult For (int from, int to, ParallelOptions options, Action<int> action)
  91. {
  92. return For (from, to, options, (index, state) => action (index));
  93. }
  94. public static ParallelLoopResult For (int from, int to, ParallelOptions options, Action<int, ParallelLoopState> action)
  95. {
  96. return For<object> (from, to, options, () => null, (i, s, l) => { action (i, s); return null; }, _ => {});
  97. }
  98. public static ParallelLoopResult For<TLocal> (int from,
  99. int to,
  100. Func<TLocal> init,
  101. Func<int, ParallelLoopState, TLocal, TLocal> action,
  102. Action<TLocal> destruct)
  103. {
  104. return For<TLocal> (from, to, ParallelOptions.Default, init, action, destruct);
  105. }
  106. public static ParallelLoopResult For<TLocal> (int from,
  107. int to,
  108. ParallelOptions options,
  109. Func<TLocal> init,
  110. Func<int, ParallelLoopState, TLocal, TLocal> action,
  111. Action<TLocal> destruct)
  112. {
  113. if (action == null)
  114. throw new ArgumentNullException ("action");
  115. if (init == null)
  116. throw new ArgumentNullException ("localInit");
  117. if (destruct == null)
  118. throw new ArgumentNullException ("localFinally");
  119. if (options == null)
  120. throw new ArgumentNullException ("options");
  121. if (from >= to)
  122. return new ParallelLoopResult (null, true);
  123. // Number of task to be launched (normally == Env.ProcessorCount)
  124. int step;
  125. int num = GetBestWorkerNumber (from, to, options, out step);
  126. Task[] tasks = new Task [num];
  127. StealRange[] ranges = new StealRange[num];
  128. for (int i = 0; i < num; i++)
  129. ranges[i] = new StealRange (from, i, step);
  130. ParallelLoopState.ExternalInfos infos = new ParallelLoopState.ExternalInfos ();
  131. int currentIndex = -1;
  132. Action workerMethod = delegate {
  133. int localWorker = Interlocked.Increment (ref currentIndex);
  134. StealRange range = ranges[localWorker];
  135. int index = range.Actual;
  136. int stopIndex = localWorker + 1 == num ? to : Math.Min (to, index + step);
  137. TLocal local = init ();
  138. ParallelLoopState state = new ParallelLoopState (infos);
  139. CancellationToken token = options.CancellationToken;
  140. try {
  141. for (int i = index; i < stopIndex; range.Actual = ++i) {
  142. if (infos.IsStopped)
  143. return;
  144. token.ThrowIfCancellationRequested ();
  145. if (infos.LowestBreakIteration != null && infos.LowestBreakIteration > i)
  146. return;
  147. state.CurrentIteration = i;
  148. local = action (i, state, local);
  149. if (i >= stopIndex - range.Stolen)
  150. break;
  151. }
  152. // Try to steal from our right neighbor (cyclic)
  153. int len = num + localWorker;
  154. for (int sIndex = localWorker + 1; sIndex < len; ++sIndex) {
  155. int extWorker = sIndex % num;
  156. range = ranges[extWorker];
  157. stopIndex = extWorker + 1 == num ? to : Math.Min (to, from + (extWorker + 1) * step);
  158. int stolen;
  159. do {
  160. stolen = range.Stolen;
  161. if (stopIndex - stolen > range.Actual)
  162. goto next;
  163. } while (Interlocked.CompareExchange (ref range.Stolen, stolen + 1, stolen) != stolen);
  164. stolen = stopIndex - stolen - 1;
  165. if (stolen > range.Actual)
  166. local = action (stolen, state, local);
  167. next:
  168. continue;
  169. }
  170. } finally {
  171. destruct (local);
  172. }
  173. };
  174. InitTasks (tasks, num, workerMethod, options);
  175. try {
  176. Task.WaitAll (tasks);
  177. } catch {
  178. HandleExceptions (tasks, infos);
  179. }
  180. return new ParallelLoopResult (infos.LowestBreakIteration, !(infos.IsStopped || infos.IsExceptional));
  181. }
  182. class StealRange
  183. {
  184. public int Stolen;
  185. public int Actual;
  186. public StealRange (int from, int i, int step)
  187. {
  188. Actual = from + i * step;
  189. }
  190. }
  191. #endregion
  192. #region Foreach
  193. static ParallelLoopResult ForEach<TSource, TLocal> (Func<int, IList<IEnumerator<TSource>>> enumerable, ParallelOptions options,
  194. Func<TLocal> init, Func<TSource, ParallelLoopState, TLocal, TLocal> action,
  195. Action<TLocal> destruct)
  196. {
  197. if (enumerable == null)
  198. throw new ArgumentNullException ("source");
  199. if (options == null)
  200. throw new ArgumentNullException ("options");
  201. if (action == null)
  202. throw new ArgumentNullException ("action");
  203. if (init == null)
  204. throw new ArgumentNullException ("init");
  205. if (destruct == null)
  206. throw new ArgumentNullException ("destruct");
  207. int num = Math.Min (GetBestWorkerNumber (),
  208. options != null && options.MaxDegreeOfParallelism != -1 ? options.MaxDegreeOfParallelism : int.MaxValue);
  209. Task[] tasks = new Task[num];
  210. ParallelLoopState.ExternalInfos infos = new ParallelLoopState.ExternalInfos ();
  211. SimpleConcurrentBag<TSource> bag = new SimpleConcurrentBag<TSource> (num);
  212. const int bagCount = 5;
  213. IList<IEnumerator<TSource>> slices = enumerable (num);
  214. int sliceIndex = -1;
  215. Action workerMethod = delegate {
  216. IEnumerator<TSource> slice = slices[Interlocked.Increment (ref sliceIndex)];
  217. TLocal local = init ();
  218. ParallelLoopState state = new ParallelLoopState (infos);
  219. int workIndex = bag.GetNextIndex ();
  220. CancellationToken token = options.CancellationToken;
  221. try {
  222. bool cont = true;
  223. TSource element;
  224. while (cont) {
  225. if (infos.IsStopped || infos.IsBroken.Value)
  226. return;
  227. token.ThrowIfCancellationRequested ();
  228. for (int i = 0; i < bagCount && (cont = slice.MoveNext ()); i++) {
  229. bag.Add (workIndex, slice.Current);
  230. }
  231. for (int i = 0; i < bagCount && bag.TryTake (workIndex, out element); i++) {
  232. if (infos.IsStopped)
  233. return;
  234. token.ThrowIfCancellationRequested ();
  235. local = action (element, state, local);
  236. }
  237. }
  238. while (bag.TrySteal (workIndex, out element)) {
  239. token.ThrowIfCancellationRequested ();
  240. local = action (element, state, local);
  241. if (infos.IsStopped || infos.IsBroken.Value)
  242. return;
  243. }
  244. } finally {
  245. destruct (local);
  246. }
  247. };
  248. InitTasks (tasks, num, workerMethod, options);
  249. try {
  250. Task.WaitAll (tasks);
  251. } catch {
  252. HandleExceptions (tasks, infos);
  253. }
  254. return new ParallelLoopResult (infos.LowestBreakIteration, !(infos.IsStopped || infos.IsExceptional));
  255. }
  256. public static ParallelLoopResult ForEach<TSource> (IEnumerable<TSource> enumerable, Action<TSource> action)
  257. {
  258. if (enumerable == null)
  259. throw new ArgumentNullException ("source");
  260. if (action == null)
  261. throw new ArgumentNullException ("action");
  262. return ForEach<TSource, object> (Partitioner.Create (enumerable),
  263. ParallelOptions.Default,
  264. () => null,
  265. (e, s, l) => { action (e); return null; },
  266. _ => {});
  267. }
  268. public static ParallelLoopResult ForEach<TSource> (IEnumerable<TSource> enumerable, Action<TSource, ParallelLoopState> action)
  269. {
  270. if (enumerable == null)
  271. throw new ArgumentNullException ("source");
  272. if (action == null)
  273. throw new ArgumentNullException ("action");
  274. return ForEach<TSource, object> (Partitioner.Create (enumerable),
  275. ParallelOptions.Default,
  276. () => null,
  277. (e, s, l) => { action (e, s); return null; },
  278. _ => {});
  279. }
  280. public static ParallelLoopResult ForEach<TSource> (IEnumerable<TSource> enumerable,
  281. Action<TSource, ParallelLoopState, long> action)
  282. {
  283. if (enumerable == null)
  284. throw new ArgumentNullException ("source");
  285. if (action == null)
  286. throw new ArgumentNullException ("action");
  287. return ForEach<TSource, object> (Partitioner.Create (enumerable),
  288. ParallelOptions.Default,
  289. () => null,
  290. (e, s, l) => { action (e, s, -1); return null; },
  291. _ => {});
  292. }
  293. public static ParallelLoopResult ForEach<TSource> (Partitioner<TSource> source,
  294. Action<TSource, ParallelLoopState> body)
  295. {
  296. if (body == null)
  297. throw new ArgumentNullException ("body");
  298. return ForEach<TSource, object> (source,
  299. ParallelOptions.Default,
  300. () => null,
  301. (e, s, l) => { body (e, s); return null; },
  302. _ => {});
  303. }
  304. public static ParallelLoopResult ForEach<TSource> (OrderablePartitioner<TSource> source,
  305. Action<TSource, ParallelLoopState, long> body)
  306. {
  307. if (body == null)
  308. throw new ArgumentNullException ("body");
  309. return ForEach<TSource, object> (source,
  310. ParallelOptions.Default,
  311. () => null,
  312. (e, s, i, l) => { body (e, s, i); return null; },
  313. _ => {});
  314. }
  315. public static ParallelLoopResult ForEach<TSource> (Partitioner<TSource> source,
  316. Action<TSource> body)
  317. {
  318. if (body == null)
  319. throw new ArgumentNullException ("body");
  320. return ForEach<TSource, object> (source,
  321. ParallelOptions.Default,
  322. () => null,
  323. (e, s, l) => { body (e); return null; },
  324. _ => {});
  325. }
  326. public static ParallelLoopResult ForEach<TSource> (IEnumerable<TSource> source,
  327. ParallelOptions parallelOptions,
  328. Action<TSource> body)
  329. {
  330. if (source == null)
  331. throw new ArgumentNullException ("source");
  332. if (body == null)
  333. throw new ArgumentNullException ("body");
  334. return ForEach<TSource, object> (Partitioner.Create (source),
  335. parallelOptions,
  336. () => null,
  337. (e, s, l) => { body (e); return null; },
  338. _ => {});
  339. }
  340. public static ParallelLoopResult ForEach<TSource> (IEnumerable<TSource> source, ParallelOptions parallelOptions,
  341. Action<TSource, ParallelLoopState> body)
  342. {
  343. if (source == null)
  344. throw new ArgumentNullException ("source");
  345. if (body == null)
  346. throw new ArgumentNullException ("body");
  347. return ForEach<TSource, object> (Partitioner.Create (source),
  348. parallelOptions,
  349. () => null,
  350. (e, s, l) => { body (e, s); return null; },
  351. _ => {});
  352. }
  353. public static ParallelLoopResult ForEach<TSource> (IEnumerable<TSource> source, ParallelOptions parallelOptions,
  354. Action<TSource, ParallelLoopState, long> body)
  355. {
  356. if (source == null)
  357. throw new ArgumentNullException ("source");
  358. if (body == null)
  359. throw new ArgumentNullException ("body");
  360. return ForEach<TSource, object> (Partitioner.Create (source),
  361. parallelOptions,
  362. () => null,
  363. (e, s, i, l) => { body (e, s, i); return null; },
  364. _ => {});
  365. }
  366. public static ParallelLoopResult ForEach<TSource> (OrderablePartitioner<TSource> source, ParallelOptions parallelOptions,
  367. Action<TSource, ParallelLoopState, long> body)
  368. {
  369. if (body == null)
  370. throw new ArgumentNullException ("body");
  371. return ForEach<TSource, object> (source,
  372. parallelOptions,
  373. () => null,
  374. (e, s, i, l) => { body (e, s, i); return null; },
  375. _ => {});
  376. }
  377. public static ParallelLoopResult ForEach<TSource> (Partitioner<TSource> source, ParallelOptions parallelOptions,
  378. Action<TSource> body)
  379. {
  380. if (body == null)
  381. throw new ArgumentNullException ("body");
  382. return ForEach<TSource, object> (source,
  383. parallelOptions,
  384. () => null,
  385. (e, s, l) => { body (e); return null; },
  386. _ => {});
  387. }
  388. public static ParallelLoopResult ForEach<TSource> (Partitioner<TSource> source, ParallelOptions parallelOptions,
  389. Action<TSource, ParallelLoopState> body)
  390. {
  391. return ForEach<TSource, object> (source,
  392. parallelOptions,
  393. () => null,
  394. (e, s, l) => { body (e, s); return null; },
  395. _ => {});
  396. }
  397. public static ParallelLoopResult ForEach<TSource, TLocal> (IEnumerable<TSource> source, Func<TLocal> localInit,
  398. Func<TSource, ParallelLoopState, TLocal, TLocal> body,
  399. Action<TLocal> localFinally)
  400. {
  401. if (source == null)
  402. throw new ArgumentNullException ("source");
  403. return ForEach<TSource, TLocal> ((Partitioner<TSource>)Partitioner.Create (source),
  404. ParallelOptions.Default,
  405. localInit,
  406. body,
  407. localFinally);
  408. }
  409. public static ParallelLoopResult ForEach<TSource, TLocal> (IEnumerable<TSource> source, Func<TLocal> localInit,
  410. Func<TSource, ParallelLoopState, long, TLocal, TLocal> body,
  411. Action<TLocal> localFinally)
  412. {
  413. return ForEach<TSource, TLocal> (Partitioner.Create (source),
  414. ParallelOptions.Default,
  415. localInit,
  416. body,
  417. localFinally);
  418. }
  419. public static ParallelLoopResult ForEach<TSource, TLocal> (OrderablePartitioner<TSource> source, Func<TLocal> localInit,
  420. Func<TSource, ParallelLoopState, long, TLocal, TLocal> body,
  421. Action<TLocal> localFinally)
  422. {
  423. return ForEach<TSource, TLocal> (source, ParallelOptions.Default, localInit, body, localFinally);
  424. }
  425. public static ParallelLoopResult ForEach<TSource, TLocal> (Partitioner<TSource> source, Func<TLocal> localInit,
  426. Func<TSource, ParallelLoopState, TLocal, TLocal> body,
  427. Action<TLocal> localFinally)
  428. {
  429. return ForEach<TSource, TLocal> (source, ParallelOptions.Default, localInit, body, localFinally);
  430. }
  431. public static ParallelLoopResult ForEach<TSource, TLocal> (IEnumerable<TSource> source, ParallelOptions parallelOptions,
  432. Func<TLocal> localInit,
  433. Func<TSource, ParallelLoopState, TLocal, TLocal> body,
  434. Action<TLocal> localFinally)
  435. {
  436. if (source == null)
  437. throw new ArgumentNullException ("source");
  438. return ForEach<TSource, TLocal> (Partitioner.Create (source), parallelOptions, localInit, body, localFinally);
  439. }
  440. public static ParallelLoopResult ForEach<TSource, TLocal> (IEnumerable<TSource> source, ParallelOptions parallelOptions,
  441. Func<TLocal> localInit,
  442. Func<TSource, ParallelLoopState, long, TLocal, TLocal> body,
  443. Action<TLocal> localFinally)
  444. {
  445. if (source == null)
  446. throw new ArgumentNullException ("source");
  447. return ForEach<TSource, TLocal> (Partitioner.Create (source), parallelOptions, localInit, body, localFinally);
  448. }
  449. public static ParallelLoopResult ForEach<TSource, TLocal> (Partitioner<TSource> enumerable, ParallelOptions options,
  450. Func<TLocal> init,
  451. Func<TSource, ParallelLoopState, TLocal, TLocal> action,
  452. Action<TLocal> destruct)
  453. {
  454. if (enumerable == null)
  455. throw new ArgumentNullException ("source");
  456. if (action == null)
  457. throw new ArgumentNullException ("action");
  458. return ForEach<TSource, TLocal> (enumerable.GetPartitions, options, init, action, destruct);
  459. }
  460. public static ParallelLoopResult ForEach<TSource, TLocal> (OrderablePartitioner<TSource> enumerable, ParallelOptions options,
  461. Func<TLocal> init,
  462. Func<TSource, ParallelLoopState, long, TLocal, TLocal> action,
  463. Action<TLocal> destruct)
  464. {
  465. if (enumerable == null)
  466. throw new ArgumentNullException ("source");
  467. if (action == null)
  468. throw new ArgumentNullException ("action");
  469. return ForEach<KeyValuePair<long, TSource>, TLocal> (enumerable.GetOrderablePartitions,
  470. options,
  471. init,
  472. (e, s, l) => action (e.Value, s, e.Key, l),
  473. destruct);
  474. }
  475. #endregion
  476. #region Invoke
  477. public static void Invoke (params Action[] actions)
  478. {
  479. if (actions == null)
  480. throw new ArgumentNullException ("actions");
  481. Invoke (actions, (Action a) => Task.Factory.StartNew (a));
  482. }
  483. public static void Invoke (ParallelOptions parallelOptions, params Action[] actions)
  484. {
  485. if (parallelOptions == null)
  486. throw new ArgumentNullException ("parallelOptions");
  487. if (actions == null)
  488. throw new ArgumentNullException ("actions");
  489. Invoke (actions, (Action a) => Task.Factory.StartNew (a, parallelOptions.CancellationToken, TaskCreationOptions.None, parallelOptions.TaskScheduler));
  490. }
  491. static void Invoke (Action[] actions, Func<Action, Task> taskCreator)
  492. {
  493. if (actions.Length == 0)
  494. throw new ArgumentException ("actions is empty");
  495. // Execute it directly
  496. if (actions.Length == 1 && actions[0] != null)
  497. actions[0] ();
  498. bool shouldThrow = false;
  499. Task[] ts = Array.ConvertAll (actions, delegate (Action a) {
  500. if (a == null) {
  501. shouldThrow = true;
  502. return null;
  503. }
  504. return taskCreator (a);
  505. });
  506. if (shouldThrow)
  507. throw new ArgumentException ("One action in actions is null", "actions");
  508. try {
  509. Task.WaitAll (ts);
  510. } catch {
  511. HandleExceptions (ts);
  512. }
  513. }
  514. #endregion
  515. #region SpawnBestNumber, used by PLinq
  516. internal static Task[] SpawnBestNumber (Action action, Action callback)
  517. {
  518. return SpawnBestNumber (action, -1, callback);
  519. }
  520. internal static Task[] SpawnBestNumber (Action action, int dop, Action callback)
  521. {
  522. return SpawnBestNumber (action, dop, false, callback);
  523. }
  524. internal static Task[] SpawnBestNumber (Action action, int dop, bool wait, Action callback)
  525. {
  526. // Get the optimum amount of worker to create
  527. int num = dop == -1 ? (wait ? GetBestWorkerNumber () + 1 : GetBestWorkerNumber ()) : dop;
  528. // Initialize worker
  529. CountdownEvent evt = new CountdownEvent (num);
  530. Task[] tasks = new Task [num];
  531. for (int i = 0; i < num; i++) {
  532. tasks [i] = Task.Factory.StartNew (() => {
  533. action ();
  534. evt.Signal ();
  535. if (callback != null && evt.IsSet)
  536. callback ();
  537. });
  538. }
  539. // If explicitely told, wait for all workers to complete
  540. // and thus let main thread participate in the processing
  541. if (wait)
  542. Task.WaitAll (tasks);
  543. return tasks;
  544. }
  545. #endregion
  546. }
  547. }
  548. #endif