Search code examples
c#task-parallel-librarytpl-dataflow

Is the TPL Dataflow SingleProducerConstrained referring to number of source block or the parallelism degree of them?


Is the TPL Dataflow SingleProducerConstrained option referring to the number of source blocks or the minimum total parallelism degree of the source blocks?

i.e. if I only have one source block linking to a block with this option, must the MaxDegreeOfParallelism of the source be 1 or not?


Solution

  • No, it means that (from MSDN):

    methods like Post, Complete, Fault, and OfferMessage will never be called concurrently.

    So you should set this property to true (false is the default value) if you're 100%-sure that the block will get messages in it from only one source at a given moment. Examples:

    • Block is target for only one linked source
    • You use lock around all the method sending something to the block
    • your app is single-threaded, and you sending messages not using the thread-pool or some other threading techniques.
    • etc.

    Now back to your question:

    if I only have one source block linking to a block with this option, must the MaxDegreeOfParallelism of the source be 1 or not?

    It should be 1, as if it will be more than that, it can ruin some checks which are dropped with SingleProducerConstrained set to true.