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 ".