Michael Kiros
Michael Kiros

Reputation: 583

Windows concurrency runtime task scheduling with exceptions

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;
}

Upvotes: 0

Views: 495

Answers (1)

Cameron Hall
Cameron Hall

Reputation: 46

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
});

Upvotes: 2

Related Questions