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.