Since C++11 doesn't have a future.then
I've started using concurrency::task
from the Microsoft PPL
library. It works great most of the time.
However, right now I'm in a situation where I'm doing GPGPU, so having .then continuations scheduled in the PPL
scheduler causes unnecessary delay where the GPU is idle.
My question is whether there is any possible workaround for concurrency::task
and concurrency::task::then
to have them executed directly.
From my understanding a regularly scheduled task will in most cases execute it's continuation right away due to cache efficiency reasons. However, this is not the case for tasks that have been scheduled from an explicit thread (i.e. the GPU thread) using concurrency::task_completion_event
.
An example of what I am doing:
template<typename F>
auto execute(F f) -> concurrency::task<decltype(f())>
{
concurrency::task_completion_event<decltype(f())> e;
gpu_execution_queue_.push([=]
{
try
{
e.set(copy(f())); // Skipped meta-template programming for void.
}
catch(...)
{
e.set_exception(std::current_exception());
}
});
// Any continuation will be delayed since it will first be
// enqueued into the task-scheduler.
return concurrency::task<decltype(f())>(std::move(e));
}
void foo()
{
std::vector<char> data /* = ... */;
execute([=]() -> texture
{
return copy(data)
})
.then(concurrency::task<texture> t)
{
return execute([=]
{
render(t.get());
});
})
.get();
}