Search code examples
c#tpl-dataflow

Greedy JoinBlock explanation


In the default greedy mode, all data offered to targets are accepted, even if the other target doesn’t have the necessary data with which to form a tuple

According to this my understanding is that I could receive using the next snippet.

var block = new JoinBlock<int,int>(new GroupingDataflowBlockOptions(){Greedy = true});
block.Target1.Post(1);
//block.Target2.Post(2);
var tuple = block.Receive();

Can someone please explain to me why I need to post data to both targets?


Solution

  • The block will accept data to one target but it will not propagate the data until it can form a Tuple. In other words you can fill target1 with as much as you want but if there's nothing at target2 you'll never get anything out.

    Greedy behavior can be an issue if you have distributed the load across multiple join blocks. One block would suck up all the input data without letting the others have a chance.

    The use of non-greedy joins can also help you prevent deadlock in your application. In a software application, deadlock occurs when two or more processes each hold a resource and mutually wait for another process to release some other resource. Consider an application that defines two JoinBlock objects. Both objects each read data from two shared source blocks. In greedy mode, if one join block reads from the first source and the second join block reads from the second source, the application might deadlock because both join blocks mutually wait for the other to release its resource. In non-greedy mode, each join block reads from its sources only when all data is available, and therefore, the risk of deadlock is eliminated.

    Source: How to: Use JoinBlock to Read Data From Multiple Sources