batchblock greedy code example

Example: batchblock greedy

Task<TOutput> GetValueFromFastest<TInput, TOutput>(TInput input, Func<TInput, Task<TOutput>> algorithm1,
    Func<TInput, Task<TOutput>> algorithm2)
{
    var broadcastBlock = new BroadcastBlock<TInput>(null);
    var transformBlock1 = new TransformBlock<TInput, TOutput>(algorithm1);
    var transformBlock2 = new TransformBlock<TInput, TOutput>(algorithm2);
    broadcastBlock.LinkTo(transformBlock1);
    broadcastBlock.LinkTo(transformBlock2);
    var writeOnceBlock = new WriteOnceBlock<TOutput>(null);
    transformBlock1.LinkTo(writeOnceBlock);
    transformBlock2.LinkTo(writeOnceBlock);


    broadcastBlock.Post(input);

    return writeOnceBlock.ReceiveAsync();
}