Parallel Programming-pipeline for implementing Parallel operations (producers and consumers ),

Source: Internet
Author: User

Parallel Programming-pipeline for implementing Parallel operations (producers and consumers ),

This article describes how to use C # To implement a parallel execution pipeline (producer and consumer ):

1. Assembly Line

2. Implementing parallel pipelines

I. Assembly Line

 

Demonstrates the pipeline where action1 receives input and then generates results stored in buffer1. action2 reads the data generated by action1 in buffer1 and accordingly guides action4 to generate Output.

The above is also a typical producer-consumer model.

In the above mode, if you use a general serial execution method, it is easy to perform it step by step according to the flowchart. If you want to use parallel execution to improve efficiency, that is to say, the producer and consumer execute concurrently, what should we do?

Ii. Implementing parallel pipeline code 2.1
class PiplelineDemo    {        private int seed;        public PiplelineDemo()        {            seed = 10;        }        public void Action1(BlockingCollection<string> output)        {            try            {                for (var i = 0; i < seed; i++)                {                    output.Add(i.ToString());//initialize data to buffer1                }            }            finally            {                output.CompleteAdding();            }        }        public void Action2(BlockingCollection<string> input, BlockingCollection<string> output)        {            try            {                foreach (var item in input.GetConsumingEnumerable())                {                    var itemToInt = int.Parse(item);                    output.Add((itemToInt * itemToInt).ToString());// add new data to buffer2                }            }            finally            {                output.CompleteAdding();            }        }        public void Action3(BlockingCollection<string> input, BlockingCollection<string> output)        {            try            {                foreach (var item in input.GetConsumingEnumerable())                {                    output.Add(item);//set data into buffer3                }            }            finally            {                output.CompleteAdding();            }        }        public void Pipeline()        {            var buffer1 = new BlockingCollection<string>(seed);            var buffer2 = new BlockingCollection<string>(seed);            var buffer3 = new BlockingCollection<string>(seed);            var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);            var stage1 = taskFactory.StartNew(() => Action1(buffer1));            var stage2 = taskFactory.StartNew(() => Action2(buffer1, buffer2));            var stage3 = taskFactory.StartNew(() => Action3(buffer2, buffer3));            Task.WaitAll(stage1, stage2, stage3);            foreach(var item in buffer3.GetConsumingEnumerable())//print data in buffer3            {                Console.WriteLine(item);            }        }    }    class Program    {        static void Main(string[] args)        {            new PiplelineDemo().Pipeline();            Console.Read();        }    }
2.2 running results

The result of self-multiplication of 0-9 is expected to be printed.

2.3 code explanation

The logic of the Code itself corresponds to the flowchart at the beginning of this article.

BlockingCollection <T> is a thread security set in. Net. IProducerConsumerCollection <T>.

The above motion graphs are provided by "Graph dado ".

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.