none
How to sequence two pre-created tasks with a continuation RRS feed

  • Question

  • I'm a seasoned developer, but PPL is not my strong suit.

    How do I sequence two tasks that are already created?

    auto t1 = concurrency::create_task( [] { print( "Hello" ); } );
    auto t2 = concurrency::create_task( [] { print( "World" ); } );
    // Here's what I tried
    auto t3 = t1.then(t2); // error C2893

    error C2893: Failed to specialize function template 'unknown-type Concurrency::details::_VoidReturnTypeHelper(_Function,int,...)'

    I realize that in this example, I could have written a .then( [] { print "World" ); } ), but that's not applicable because I have two tasks that are already created.  I want to create a task that does one of the pre-created tasks before the other and I was expecting an obvious way to construct the continuation.

    Monday, January 4, 2016 9:39 PM

Answers

  • Seems that then takes a function or lambda, which is supposed to receive the result of the previous task.

    In case of void, this alternative can be considered:

        t1.wait(); t2.wait();

    or

        auto t3 = t1.then( [=] { t2.wait(); } );

    or

        auto t3 = t1.then( [=] { t2.then( [ ] { } ); } );

    or

        auto t3 = concurrency::create_task( [=] { t1.wait(); t2.wait(); } );

    • Marked as answer by Wyck Tuesday, January 5, 2016 2:39 PM
    Tuesday, January 5, 2016 7:20 AM

All replies

  • Seems that then takes a function or lambda, which is supposed to receive the result of the previous task.

    In case of void, this alternative can be considered:

        t1.wait(); t2.wait();

    or

        auto t3 = t1.then( [=] { t2.wait(); } );

    or

        auto t3 = t1.then( [=] { t2.then( [ ] { } ); } );

    or

        auto t3 = concurrency::create_task( [=] { t1.wait(); t2.wait(); } );

    • Marked as answer by Wyck Tuesday, January 5, 2016 2:39 PM
    Tuesday, January 5, 2016 7:20 AM
  • @Viorel_ Those are all good ideas for workarounds.  Thanks.

    Of those, auto t3 = t1.then( [=] { t2.wait(); } )  is most appropriate for me.

    Tuesday, January 5, 2016 2:39 PM