Created
December 29, 2019 23:03
-
-
Save AlgorithmsAreCool/fe01df36be764bf444238072fb29a351 to your computer and use it in GitHub Desktop.
Repro program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Threading.Tasks.Dataflow; | |
namespace randomTPLRepro | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
await Buffer(); | |
} | |
private static async Task BufferBroadcast() | |
{ | |
var ab1Count = 0; | |
var ab2Count = 0; | |
var edbo = new ExecutionDataflowBlockOptions(); | |
var buffer = new BufferBlock<int>(); | |
var broadcast = new BroadcastBlock<int>(x => x); | |
var lo = new DataflowLinkOptions { PropagateCompletion = true }; | |
var tb1 = new TransformBlock<int, string>(x => x.ToString(), edbo); | |
var ab1 = new ActionBlock<string>(x => Interlocked.Increment(ref ab1Count), edbo); | |
var tb2 = new TransformBlock<int, string>(x => x.ToString(), edbo); | |
var ab2 = new ActionBlock<string>(x => Interlocked.Increment(ref ab2Count), edbo); | |
var link0 = buffer.LinkTo(broadcast, lo); | |
var link1 = broadcast.LinkTo(tb1, lo); | |
var link2 = tb1.LinkTo(ab1, lo); | |
var link3 = broadcast.LinkTo(tb2, lo); | |
var link4 = tb2.LinkTo(ab2, lo); | |
for (var i = 0; i < 10; ++i) | |
{ | |
if (!await buffer.SendAsync(i)) | |
throw new Exception("Send Failed"); | |
} | |
buffer.Complete(); | |
await ab1.Completion; | |
await ab2.Completion; | |
Console.WriteLine($"Count 1 = {ab1Count}"); | |
Console.WriteLine($"Count 2 = {ab2Count}"); | |
} | |
private static async Task Buffer() | |
{ | |
var ab1Count = 0; | |
var ab2Count = 0; | |
var bb = new BufferBlock<int>(); | |
var lo = new DataflowLinkOptions { PropagateCompletion = true }; | |
var edbo = new ExecutionDataflowBlockOptions(); | |
//edbo.BoundedCapacity = 5; | |
var tb1 = new TransformBlock<int, string>(x => x.ToString(), edbo); | |
var ab1 = new ActionBlock<string>(x => Interlocked.Increment(ref ab1Count), edbo); | |
var tb2 = new TransformBlock<int, string>(x => x.ToString(), edbo); | |
var ab2 = new ActionBlock<string>(x => Interlocked.Increment(ref ab2Count), edbo); | |
var link1 = bb.LinkTo(tb1, lo); | |
var link2 = tb1.LinkTo(ab1, lo); | |
var link3 = bb.LinkTo(tb2, lo); | |
var link4 = tb2.LinkTo(ab2, lo); | |
for (var i = 0; i < 10; ++i) | |
{ | |
if (!await bb.SendAsync(i)) | |
throw new Exception("Send Failed"); | |
} | |
bb.Complete(); | |
await ab1.Completion; | |
await ab2.Completion; | |
Console.WriteLine($"Count 1 = {ab1Count}"); | |
Console.WriteLine($"Count 2 = {ab2Count}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output from Buffer:
Output from BufferBroadcast: