Running and awaiting a blocking background task from within a lely fiber
What is a thread-safe way of executing a long-running, blocking task in a background thread and retrieving its result?
Is it as simple as passing a lely::canopen::SdoPromise to a background thread, followed by a BasicDriver::Wait(promise.get_future())?
Here's my sample code:
class BlockingTask {
public:
BlockingTask(const std::function<T()> &func)
:
thread{[this, func](){ promise.set(func()); }} {}
~BlockingTask() { thread.join(); }
inline lely::canopen::SdoFuture<T> get_future() {
return promise.get_future();
}
private:
lely::canopen::SdoPromise<T> promise;
std::thread thread;
};
and e.g. in FiberDriver::OnBoot:
BlockingTask<int> task([](){
return perform_long_running_blocking_action();
});
Wait(task.get_future());
But I'm not sure whether accessing the promise object is actually legal from outside a running fiber, and whether the executor will actually wake up in time when the result to a promise is set from outside a running fiber.
Also, maybe I have overlooked a really simple solution to the problem... Thank you!