贪心 JoinBlock 解释

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

据此,我的理解是我可以使用下一个片段接收。

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

有人可以向我解释为什么我需要 post 数据到两个目标吗?

该块将接受一个目标的数据,但它不会传播数据,直到它可以形成 Tuple。换句话说,你可以随心所欲地填充 target1,但如果 target2 中没有任何东西,你将永远不会得到任何东西。

如果您已将负载分布到多个连接块,贪婪行为可能会成为一个问题。一个块会吸收所有输入数据,而不会让其他块有机会。

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