How to time thread calls and returns within a frame/process cycle

Godot Version

4.3

Question

I’m using separate Threads to avoid doing some processing on the main thread, typically initiating these threaded separate processes in response to user input events. Once the thread completes this work I use the .call_deferred() method to call a “work done” method back on the main thread that collects the result and displays it in the UI (that only the main thread can do).

The issue I’m having with this is that even if the thread work finishes very quickly the call_deferred return method means the result will be made available to the main thread the next frame, leading to a one frame delay, like so:

FRAME 1 START → EVENT → THREAD START → THREAD FINISH → RENDER FRAME 1
FRAME 2 START → DEFERRED THREAD RESULT ON MAIN THREAD-> RENDER FRAME 2

I’m wondering now how can I avoid this and get access to the result of the separate thread on the main thread in the same frame if it’s finished before the vsync frame time:

FRAME 1 START → EVENT → THREAD START → THREAD FINISH → THREAD RESULT ON MAIN THREAD → RENDER FRAME 1

Basically start the separate thread processes like I do now based on input events. But then before the frame is sent to the GPU get another chance to check if the started thread has already finished and has a result we can use right away in the same frame.

not possible without freezing main thread if you want a result in the same frame.
wait_to_finish to freeze, or call_deferred and collect result on next frame,
I think you may try to use WorkerThreadPool and waiting on _physics_process(), Godot has project’s option to run physics on separate thread, that where you can possibly yield threads.
But better not try to use processed thread data on same frame