Microsoft's CCR vs Task Parallel Library

后端 未结 2 1938
耶瑟儿~
耶瑟儿~ 2021-01-01 00:37

Microsoft has at least two different approches to improved support for concurrent operations.

1) Is the Concurrency Coordination Runtime (CCR) which is part of Micro

相关标签:
2条回答
  • 2021-01-01 00:49

    By and large, both frameworks have complementary but different goals.

    The CCR offers primitives for coordination of concurrent processes. Coordination is the glue that makes a bunch of processes work as a whole - so the CCR offers primitives for exchanging messages through so called channels. Processes can wait for a message to arrive on a channel, or a number of channels, or any one of a number of channels and so forth. This is a particular paradigm for coordination of concurrent processes that works well. Note also that is it not free - you have to buy if from Microsoft separately.

    The TPL offers primitives and infrastructure to parallellize computations or algorithms semi-automatically. One of the most obvious primitives there is the parallel for loop - looks sort of like a for loop but tries to execute the loop in parallel.

    So, if you have a bunch of process that you'd like to coordinate on a higher level than using shared state and locks, use the CCR. If you have a computation-intensive process that you'd like to run efficiently on a multi-core machine, use the TPL.

    0 讨论(0)
  • 2021-01-01 01:05

    It's not an either/or scenario. The CCR is a library that supports certain programming patterns. You can intermix CCR and TPL code like this, here is a Parallel.For inside of a Receive delegate:

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Ccr.Core;
    
    namespace Demo
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                Dispatcher dispatcher = new Dispatcher();
                DispatcherQueue taskQueue = new DispatcherQueue("Demo", dispatcher);
                Port<int> portInt = new Port<int>();
                portInt.Post(Int32.Parse(args[0]));
    
                Arbiter.Activate(
                    taskQueue,
                    portInt.Receive(delegate(int count)
                    {
                        Parallel.For(0, count, i =>
                        {
                            Console.Write(i.ToString() + " ");
                        });
                    }
                ));
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题