SingleProducerConstrained and MaxDegreeOfParalleli

2020-06-03 03:53发布

问题:

In the C# TPL Dataflow library, SingleProducerConstrained is an optimisation option for ActionBlocks you can use when only a single thread is feeding the action block:

If a block is only ever going to be used by a single producer at a time, meaning only one thread at a time will be using methods like Post, OfferMessage, and Complete on the block, this property may be set to true to inform the block that it need not apply extra synchronization.

What if an ActionBlock is fed using a single TransformBlock which have MaxDegreeOfPArallelism > 1 - would that violate the rules for setting SingleProcerContrained to true on the ActionBlock? or is a single TransformBlock with MaxDegreeOfPArallelism > 1 still counted as a "single producer"?

回答1:

Yes, I think it's considered a single producer.

This is because all the built-int blocks maintain ordering, so item 2 can be offered (using the OfferMessage() method) to the target block only after item 1 has been accepted. And this constraint means that there can be only one thread offering messages to the target at a time, which follows the definition of "single producer".