简体   繁体   中英

Joining task-parallel threads

I know how to join a simple thread to make parent thread wait for them but I don't know how to do it for automatic threads implemented in System.Threading.Tasks namespace . Here's an example . I call two functions in my main function that I want to be executed respectively . Each function has an parallel.for inside it . Now how can I make the main thread wait for all threads of first function to get finished before calling second function ?

static void Main(string[] args)
{
     fill();
     // now main should wait for threads of fill function to finish
     filter();
}

void filter()
{
     parallel.for(some action);
}

void fill()
{
    parallel.for(some action);
}

Parallel.For Method internally fork iteration among multiple threads, but then it blocks till they all finish. Therefore your code is a sequence code in point of view of the main function:

we fork the processing of loop iterations, and we join such that the parallel loop invocation only completes when all concurrent processing is done.

If you need some local state shared over the loop, use Parallel.For Method (Int32, Int32, Func, Func, Action) version of the function:

public static ParallelLoopResult For<TLocal>(
    int fromInclusive,
    int toExclusive,
    Func<TLocal> localInit,
    Func<int, ParallelLoopState, TLocal, TLocal> body,
    Action<TLocal> localFinally
)

The below image depict how it works:

在此输入图像描述

Here is a sample implementation of calculating parallel PI:

static double ParallelPi()
{
    double sum = 0.0;
    double step = 1.0 / (double)NUM_STEPS;
    object obj = new object();
    Parallel.For(0, NUM_STEPS,
    () => 0.0,
    (i, state, partial) =>
    {
        double x = (i + 0.5) * step;
        return partial + 4.0 / (1.0 + x * x);
    },
    partial => { lock (obj) sum += partial; });
    return step * sum;
}

For depth information about how Parallel class works along with examples, read Patterns for Parallel Programming: Understanding and Applying Parallel Patterns with the .NET Framework 4 (Blackquote, image and example are taken from there)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM