concept step in category .net

This is an excerpt from Manning's book Concurrency in .NET.
Sequential programming is the act of accomplishing things in steps. Let’s consider a simple example, such as getting a cup of cappuccino at the local coffee shop. You first stand in line to place your order with the lone barista. The barista is responsible for taking the order and delivering the drink; moreover, they are able to make only one drink at a time so you must wait patiently—or not—in line before you order. Making a cappuccino involves grinding the coffee, brewing the coffee, steaming the milk, frothing the milk, and combining the coffee and milk, so more time passes before you get your cappuccino. Figure 1.1 shows this process.
Figure 1.1 For each person in line, the barista is sequentially repeating the same set of instructions (grind coffee, brew coffee, steam milk, froth milk, and combine the coffee and the milk to make a cappuccino).
![]()
Figure 1.1 is an example of sequential work, where one task must be completed before the next. It is a convenient approach, with a clear set of systematic (step-by-step) instructions of what to do and when to do it. In this example, the barista will likely not get confused and make any mistakes while preparing the cappuccino because the steps are clear and ordered. The disadvantage of preparing a cappuccino step-by-step is that the barista must wait during parts of the process. While waiting for the coffee to be ground or the milk to be frothed, the barista is effectively inactive (blocked). The same concept applies to sequential and concurrent programming models. As shown in figure 1.2, sequential programming involves a consecutive, progressively ordered execution of processes, one instruction at a time in a linear fashion.
13.2.1 Solution: composing a pipeline of steps forming the Fork/Join pattern
With TDF, you can compose different building blocks together as a pipeline. You can use the pipeline to define the steps of a Fork/Join pattern (figure 13.5), where the Fork step runs a set of tasks in parallel, then the following step joins the results, and the final step applies a reducer block for the ultimate output. For the later step of the workflow that aggregates the results, you need an object that maintains the state of the previous steps. In this case, you use the agent-based block built in chapter 12 using TDF.
Listing 13.2 Parallel
ForkJoinusing TDFpublic static async Task<R> ForkJoin<T1, T2, R>( this IEnumerable<T1> source, Func<T1, Task<IEnumerable<T2>>> map, #1 Func<R, T2, Task<R>> aggregate, #1 R initialState, CancellationTokenSource cts = null, int partitionLevel = 8, int boundCapacity = 20) #2 { cts = cts ?? new CancellationTokenSource(); var blockOptions = new ExecutionDataflowBlockOptions { #3 MaxDegreeOfParallelism = partitionLevel, BoundedCapacity = boundCapacity, CancellationToken = cts.Token }; var inputBuffer = new BufferBlock<T1>( #4 new DataflowBlockOptions { CancellationToken = cts.Token, BoundedCapacity = boundCapacity }); var mapperBlock = new TransformManyBlock<T1, T2> ➥ (map, blockOptions); #4 var reducerAgent = Agent.Start(initialState, aggregate, cts); #4 var linkOptions = new DataflowLinkOptions{PropagateCompletion=true}; inputBuffer.LinkTo(mapperBlock, linkOptions); #5 IDisposable disposable = mapperBlock.AsObservable() .Subscribe(async item => await reducerAgent.Send(item)); #6 foreach (var item in source) await inputBuffer.SendAsync(item); #7 inputBuffer.Complete(); var tcs = new TaskCompletionSource<R>(); await inputBuffer.Completion.ContinueWith(task => mapperBlock.Complete()); await mapperBlock.Completion.ContinueWith(task => { #8 var agent = reducerAgent as StatefulDataflowAgent<R, T2>; disposable.Dispose(); tcs.SetResult(agent.State); }); return await tcs.Task; } #1 The functions map and aggregate return a Task type to ensure concurrent behavior. #2 The partitionLevel is set to a default value of 8 and the boundCapacity to 20; these are arbitrary values that can, and should, be updated according to your needs. #3 Instances of the building blocks that are shaping the Fork/Join pipeline #4 Packages up the properties of the execution details to configure the inputBuffer BufferBlock #5 Connects the building blocks to form and link the steps to run the Fork/Join pattern #6 The TransformManyBlock is transformed into an Observable, which is used to push the outputs as a message to the reducerAgent. #7 Starts the Fork/Join process by pushing the items of the input collection into the first step of the pipeline #8 When the mapperBlock is completed, the continuation Task sets the reducerAgent as a result for the tcs Task passed to the caller as output. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":28},{\"line\":0,\"ch\":36}]]"} !@%STYLE%@!