tcb
tcb

Reputation: 4604

how to tell the c++ concurrency runtime to reuse the previous thread for task continuations

I used the visual c++ concurrency runtime to create a task and then scheduled four continuations on it

#include <iostream>
#include <thread>
#include <ppltasks.h>

int main()
{
    concurrency::create_task([]
    {
        std::cout << std::this_thread::get_id() << std::endl;
    })
    .then([]
    {
        std::cout << std::this_thread::get_id() << std::endl;
    })
    .then([]
    {
        std::cout << std::this_thread::get_id() << std::endl;
    })
    .then([]
    {
        std::cout << std::this_thread::get_id() << std::endl;
    })
    .then([]
    {
        std::cout << std::this_thread::get_id() << std::endl;
    });

    std::cin.get();
}

This prints the following output

29432
29432
25096
25668
42488

Notice the 4 continuations are not scheduled on the same thread as the initial task. Is there a way to schedule the continuations on the same thread as the initial task? I believe this is possible in c# by using TaskContinuationOptions.ExecuteSynchronously option.

Upvotes: 4

Views: 882

Answers (1)

Barnett Trzcinski
Barnett Trzcinski

Reputation: 1

Ordinarily, you can control the context in which continuations execute using task_continuation_context, as the MSDN documentation describes. However, the same documentation also mentions that:

It is only useful to use this class from a Windows Store app. For non-Windows Store apps, the task continuation's execution context is determined by the runtime, and not configurable.

It seems from your code snippet that while you are using the Concurrency runtime you are not using it from a Windows Store app. Therefore, the context will effectively always be arbitrary.

There's also the question of why you would want to explicitly run subsequent tasks on the same thread as the first task: why not just put those tasks' code inside the first task, then? The point of continuations is to come back to a particular thread in order to continue work that completed in a background task - meaning, the first task will necessarily be the background work you wish to do, and the continuations are reacting to that work from the main, initiating thread. If you wish to stay on the background thread, then stay there and don't bother putting that work in a continuation. (However, as mentioned above, since this isn't a Windows Store app, all of these continuations and tasks run in an arbitrary context; the runtime will just choose an available thread that's convenient.)

Upvotes: 0

Related Questions