ParallelApply: Distribute Calculations Over Multicore / Processors

This code applies a BackgroundFunction to elements of an IEnumerable using the ThreadPool. If you don't know what that means, it's probably not of interest to you:

::: {style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"} [   10]{style="COLOR: #2b91af"}     [delegate]{style="COLOR: blue"} [void]{style="COLOR: blue"} [BackgroundFunction]{style="COLOR: teal"}([object]{style="COLOR: blue"} memberOfEnumerable);

[   11]{style="COLOR: #2b91af"} 

[   12]{style="COLOR: #2b91af"}     [class]{style="COLOR: blue"} [Program]{style="COLOR: teal"}

[   13]{style="COLOR: #2b91af"}     {

[   14]{style="COLOR: #2b91af"}         [static]{style="COLOR: blue"} [void]{style="COLOR: blue"} Main([string]{style="COLOR: blue"}[] args)

[   15]{style="COLOR: #2b91af"}         {

[   16]{style="COLOR: #2b91af"}             [int]{style="COLOR: blue"}[] nums = [new]{style="COLOR: blue"} [int]{style="COLOR: blue"}[100];

[   17]{style="COLOR: #2b91af"}             [for]{style="COLOR: blue"} ([int]{style="COLOR: blue"} i = 0; i \< 100; i++)

[   18]{style="COLOR: #2b91af"}             {

[   19]{style="COLOR: #2b91af"}                 nums[i] = i;

[   20]{style="COLOR: #2b91af"}             }

[   21]{style="COLOR: #2b91af"} 

[   22]{style="COLOR: #2b91af"}             [Random]{style="COLOR: teal"} r = [new]{style="COLOR: blue"} [Random]{style="COLOR: teal"}();

[   23]{style="COLOR: #2b91af"}             [BackgroundFunction]{style="COLOR: teal"} func = [delegate]{style="COLOR: blue"}([object]{style="COLOR: blue"} memberOfEnumerable)

[   24]{style="COLOR: #2b91af"}             {

[   25]{style="COLOR: #2b91af"}                 [int]{style="COLOR: blue"} i = ([int]{style="COLOR: blue"}) memberOfEnumerable;

[   26]{style="COLOR: #2b91af"}                 [Thread]{style="COLOR: teal"}.Sleep(r.Next(1000));

[   27]{style="COLOR: #2b91af"}                 [Console]{style="COLOR: teal"}.WriteLine(i);

[   28]{style="COLOR: #2b91af"}             };

[   29]{style="COLOR: #2b91af"} 

[   30]{style="COLOR: #2b91af"}             ParallelApply(nums, func);

[   31]{style="COLOR: #2b91af"}             [Console]{style="COLOR: teal"}.ReadKey();

[   32]{style="COLOR: #2b91af"}         }

[   33]{style="COLOR: #2b91af"} 

[   34]{style="COLOR: #2b91af"}         [static]{style="COLOR: blue"} [void]{style="COLOR: blue"} ParallelApply([IEnumerable]{style="COLOR: teal"} enumerable, [BackgroundFunction]{style="COLOR: teal"} function)

[   35]{style="COLOR: #2b91af"}         {

[   36]{style="COLOR: #2b91af"}             [ManualResetEvent]{style="COLOR: teal"} done = [new]{style="COLOR: blue"} [ManualResetEvent]{style="COLOR: teal"}([false]{style="COLOR: blue"});

[   37]{style="COLOR: #2b91af"}             [int]{style="COLOR: blue"} doneRefCount = 1;

[   38]{style="COLOR: #2b91af"} 

[   39]{style="COLOR: #2b91af"}             [BackgroundFunction]{style="COLOR: teal"} wrappedBlock = [delegate]{style="COLOR: blue"}([object]{style="COLOR: blue"} state)

[   40]{style="COLOR: #2b91af"}             {

[   41]{style="COLOR: #2b91af"}                 function.DynamicInvoke(state);

[   42]{style="COLOR: #2b91af"}                 [int]{style="COLOR: blue"} isDone = [Interlocked]{style="COLOR: teal"}.Decrement([ref]{style="COLOR: blue"} doneRefCount);

[   43]{style="COLOR: #2b91af"}                 [if]{style="COLOR: blue"} (isDone == 0)

[   44]{style="COLOR: #2b91af"}                 {

[   45]{style="COLOR: #2b91af"}                     done.Set();

[   46]{style="COLOR: #2b91af"}                 }

[   47]{style="COLOR: #2b91af"}             };

[   48]{style="COLOR: #2b91af"} 

[   49]{style="COLOR: #2b91af"}             [WaitCallback]{style="COLOR: teal"} callback = [new]{style="COLOR: blue"} [WaitCallback]{style="COLOR: teal"}(wrappedBlock);

[   50]{style="COLOR: #2b91af"}             [foreach]{style="COLOR: blue"} ([object]{style="COLOR: blue"} o [in]{style="COLOR: blue"} enumerable)

[   51]{style="COLOR: #2b91af"}             {

[   52]{style="COLOR: #2b91af"}                 [Interlocked]{style="COLOR: teal"}.Increment([ref]{style="COLOR: blue"} doneRefCount);

[   53]{style="COLOR: #2b91af"}                 [ThreadPool]{style="COLOR: teal"}.QueueUserWorkItem(callback, o);

[   54]{style="COLOR: #2b91af"}             }

[   55]{style="COLOR: #2b91af"}             [int]{style="COLOR: blue"} isDoneLate = [Interlocked]{style="COLOR: teal"}.Decrement([ref]{style="COLOR: blue"} doneRefCount);

[   56]{style="COLOR: #2b91af"}             [if]{style="COLOR: blue"} (isDoneLate == 0)

[   57]{style="COLOR: #2b91af"}             {

[   58]{style="COLOR: #2b91af"}                 done.Set();

[   59]{style="COLOR: #2b91af"}             }

[   60]{style="COLOR: #2b91af"}             done.WaitOne();

[   61]{style="COLOR: #2b91af"}         }

[   62]{style="COLOR: #2b91af"}     } :::