dkormalev / asynqro

Futures and thread pool for C++ (with optional Qt support)
BSD 3-Clause "New" or "Revised" License
126 stars 18 forks source link

Is it possible to manually run a queued task in the current thread? #17

Open magiblot opened 4 years ago

magiblot commented 4 years ago

Hi,

I am trying to parallelize a library with a synchronous interface, preserving backwards compatibility. Even if the interface doesn't use Futures, I could still do something like this:

Frame Clip::getFrame() {
    Frame frame = /* */;
    if (task_not_ran)
        frame.plane = queue_task();
    return frame;
}

Plane Frame::getPlane() {
    while (!plane.isCompleted()) {
        wait_first_of(plane, tasks_pending);
        if (!plane.isCompleted())
            run_pending_task();
        else
            break;
    }
    return plane.result();
}

The idea is: since the current task has to wait for a result but cannot be evicted from the thread, it can run pending tasks recursively until the result is ready.

This is obviously worse than having a fully asynchronous interface, but could make it possible to parallelize already existing code and binaries.

Is such a thing possible?

Thanks.

dkormalev commented 4 years ago

It is not possible with how it works now. I can't guarantee that such thing will be supported (and when if yes), but it at least gave me some food for thoughts, thank you for this.

magiblot commented 4 years ago

Hi Dennis, thanks for the quick response. I am also looking into other libraries such as HPX, so I suggest you not to spend time implementing this unless it is actually useful to you. Cheers.