Difference between a greedy and a non-greedy dataflow block with boundedcapacity defined
Asked Answered
K

1

9

I have a BatchBlock with BoundedCapacity defined on it

var _batchBlock = new BatchBlock<int>(2, new GroupingDataflowBlockOptions
                                      {BoundedCapacity = 100 });

So if the queue capacity reaches a 100 the block postpones every message received until a spot becomes available. In this case is the batch queue considered greedy or non-greedy ?

Kolkhoz answered 16/10, 2014 at 1:6 Comment(0)
A
10

The block is greedy, but not because of how it handles the items above 100, but the ones below 2. You can set the greedy value to false (true is the default) and then the block would only actually consume items when there are enough to send a batch, until then they are postponed:

var batchBlock = new BatchBlock<int>(2, new GroupingDataflowBlockOptions
{
    Greedy = false,
    BoundedCapacity = 100.
});

The BatchBlock class operates in either greedy or non-greedy mode. In greedy mode, which is the default, a BatchBlock object accepts every message that it is offered and propagates out an array after it receives the specified count of elements. In non-greedy mode, a BatchBlock object postpones all incoming messages until enough sources have offered messages to the block to form a batch

From Dataflow (Task Parallel Library)

Aircraftman answered 16/10, 2014 at 1:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.