Last change
on this file since 1186 was 1114, checked in by iritscen, 5 years ago |
Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.
|
File size:
1.3 KB
|
Rev | Line | |
---|
[1114] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Threading;
|
---|
| 4 | using System.Threading.Tasks;
|
---|
| 5 |
|
---|
| 6 | namespace Oni
|
---|
| 7 | {
|
---|
| 8 | internal static class Parallel
|
---|
| 9 | {
|
---|
| 10 | public static void ForEach<T>(IEnumerable<T> items, Action<T> action)
|
---|
| 11 | {
|
---|
| 12 | var array = items.ToArray();
|
---|
| 13 |
|
---|
| 14 | if (array.Length == 0)
|
---|
| 15 | {
|
---|
| 16 | return;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | if (array.Length == 1)
|
---|
| 20 | {
|
---|
| 21 | action(array[0]);
|
---|
| 22 | return;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | int cpuCount = Environment.ProcessorCount;
|
---|
| 26 |
|
---|
| 27 | if (cpuCount == 1)
|
---|
| 28 | {
|
---|
| 29 | foreach (T item in array)
|
---|
| 30 | action(item);
|
---|
| 31 |
|
---|
| 32 | return;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | #if NETCORE
|
---|
| 36 | var task = Task.Run(() =>
|
---|
| 37 | {
|
---|
| 38 | for (int i = array.Length / 2; i < array.Length; i++)
|
---|
| 39 | action(array[i]);
|
---|
| 40 | });
|
---|
| 41 | #else
|
---|
| 42 | var thread = new Thread(() =>
|
---|
| 43 | {
|
---|
| 44 | for (int i = array.Length / 2; i < array.Length; i++)
|
---|
| 45 | action(array[i]);
|
---|
| 46 | });
|
---|
| 47 |
|
---|
| 48 | thread.Start();
|
---|
| 49 | #endif
|
---|
| 50 |
|
---|
| 51 | for (int i = 0; i < array.Length / 2; i++)
|
---|
| 52 | action(array[i]);
|
---|
| 53 |
|
---|
| 54 | #if NETCORE
|
---|
| 55 | task.Wait();
|
---|
| 56 | #else
|
---|
| 57 | thread.Join();
|
---|
| 58 | #endif
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.