none
What happens to messages when LinkTo filter is not met (cannot find discardMessages parameter)

    Question

  • In "Introduction to TPL Dataflow" Stephen Toub writes:

    •  LinkTo
      "... and what the behavior should be if the predicate is not met (e.g. should such a message simply be dropped, should such a message be declined and offered to other targets, etc.)"

    In some examples I see a boolean being passed to the LinkTo method after the filter predicate and a parameter discardMessages is mentioned. For instance the links in this nice example Tpl Dataflow walkthrough do not compile:

    // conditional link to from [linkBroadcaster] to [downloader]
    linkBroadcaster.LinkTo(downloader, linkFilter, true),
    // from [linkBroadcaster] to [writer]
    linkBroadcaster.LinkTo(writer, imgFilter, true),

    I cannot find that parameter in the LinkTo overloads. Has this behavior changed to some default?




    • Edited by pmeinl Monday, October 08, 2012 6:24 AM
    Saturday, October 06, 2012 12:32 PM

Answers

All replies

  • [Dimitri Dokadze gave the following answer which I received by email but  somehow did not show up here in the forum]

    linkBroadcaster.LinkTo(DataflowBlock.NullTarget<T>)

    is what you're looking for.

    Tuesday, October 09, 2012 5:08 AM
  • OK, so the parameter discardMessages has been replaced by DataflowBlock.NullTarget<T>. Which looks clearer to me too.

    When are we supposed to use DataflowBlock.NullTarget<T>? The nice example mentioned in my initial question uses discardMessages when linking from a BroadcastBlock, which confused me.

    This Dataflow deadlocks without a NullTarget:

    Dim transformBlock As New TransformBlock(Of String, String)(Function(input) input)
    Dim jpgProcessor As New ActionBlock(Of String)(Sub(input) Console.WriteLine(input))
    Dim pngProcessor As New ActionBlock(Of String)(Sub(input) Console.WriteLine(input))
    
    'This deadlocks without NullTarget
    transformBlock.LinkTo(jpgProcessor, Function(x) x.EndsWith(".jpg"))
    transformBlock.LinkTo(pngProcessor, Function(x) x.EndsWith(".png"))
    'transformBlock.LinkTo(DataflowBlock.NullTarget(Of String))
    
    transformBlock.Post("foo.jpg")
    transformBlock.Post("bar.img")
    transformBlock.Post("baz.png")

    Using a BroadcastBlock it does not deadlock without a NullTarget:

    Dim transformBlock As New TransformBlock(Of String, String)(Function(input) input)
    Dim jpgProcessor As New ActionBlock(Of String)(Sub(input) Console.WriteLine(input))
    Dim pngProcessor As New ActionBlock(Of String)(Sub(input) Console.WriteLine(input))
    
    Dim broadcastBlock As New BroadcastBlock(Of String)(Nothing)
    transformBlock.LinkTo(broadcastBlock)
    broadcastBlock.LinkTo(jpgProcessor, Function(x) x.EndsWith(".jpg"))
    broadcastBlock.LinkTo(pngProcessor, Function(x) x.EndsWith(".png"))
    
    transformBlock.Post("foo.jpg")
    transformBlock.Post("bar.img")
    transformBlock.Post("baz.png")

    The MSDN documentation http://msdn.microsoft.com/en-us/library/hh228603.aspx warns "Therefore, when you use filtering to connect multiple targets to a source, make sure that at least one target block receives each message. Otherwise, your application might deadlock." It does not make clear that a message is dropped if no targets are linked from a BroadcastBlock<T>, which Stephen mentions in TPLDataflow.docx.


    Are my conclusions correct?


    • Edited by pmeinl Saturday, October 13, 2012 4:15 AM
    Saturday, October 13, 2012 4:14 AM