0

According to MSDN,

A task-based continuation is always scheduled for execution when the antecedent task finishes, even when the antecedent task is canceled or throws an exception.

I don't understand this because I tried out the following code and the subsequent task isn't called when the first task has finished by throwing an exception. I understand why it must forward the call to the site where concurrency::task::wait is called but I don't understand what the statement on MSDN means. What am I misunderstanding?

#include <iostream>

#include <ppl.h>
#include <ppltasks.h>

int main(int argc, char* argv[])
{
    using namespace std;

    concurrency::task<void> task;
    auto task_ticket = concurrency::create_task([]()
    {
        // A continuation task executed asynchronously
        // after the previous task has completed. This
        // is executed even if the previous task fails
        // by being cancelled or throwing an exception.
        throw std::runtime_error("Hello");
    })

        .then([]()
    {
        // This should be executed even though the
        // previous task failed.
        cout << "Task (2) executed" << endl;
    });

    try
    {
        task_ticket.wait();
    }
    catch (std::exception const& e)
    {
        cout << "Exception caught\n";
    }
    return EXIT_SUCCESS;
}
Michael Kiros
  • 563
  • 4
  • 11

1 Answers1

2

You're misunderstanding Value-Based Versus Task-Based Continuations.

Given a task object whose return type is T, you can provide a value of type T or task to its continuation tasks. A continuation that takes type T is known as a value-based continuation.

Your initial call to create_task returns task<void>. The lambda you pass to .then accepts void as input (since .then([]() is equivalent to .then([](void)), therefore the continuation is value-based and does not run if the antecedent task throws.

To declare a task-based continuation, use:

auto task_ticket = concurrency::create_task([]()
{
    throw std::runtime_error("Hello");
})
.then([](task<void> antecedent_task)
{
    cout << "Task (2) executed" << endl;
    antecedent_task.get(); // re-throws std::runtime_error
});
Cameron Hall
  • 46
  • 1
  • 5
  • Thanks. Exactly what I was looking for. It should also be noted that you can't call `antecedent_task.get()` inside of the continuation task. P.S. Your code needs some fixing (missing braces in the second lambda) and I can't make edits shorter than x chars. – Michael Kiros Jul 23 '17 at 01:39
  • I just noticed the missing braces as well, thanks. You can definitely call `antecedent_task.get()` inside a continuation, as per the linked MSDN doc: "A task-based continuation is always scheduled for execution when the antecedent task finishes, even when the antecedent task is canceled or throws an exception. You can then call task::get to get the result of the antecedent task" – Cameron Hall Jul 23 '17 at 01:46
  • 1
    Aha, that's correct. I called `antecedent_task.get()` before printing thus it was re-throwing before I printed. – Michael Kiros Jul 23 '17 at 02:08