BitShares Forum
Main => Technical Support => Topic started by: HardFork on February 23, 2014, 11:32:50 pm
-
In bts_wallet/main.cpp, I noticed lots of code of the form:
main_thread->async( [=]() {
// ... do stuff ...
} ).wait();
What is the point of running async code if all you are doing is waiting for it to complete? Why not replace the above code with just:
// ... do stuff ...
-
Because blocking the main thread is a wicked thing to do?
EDIT: Ambiguated my punctuation.
-
Isn't that what .wait() is doing? It blocks the main thread to wait for the other thread to complete.
-
It has to do with capturing errors from the call. I post the call back to the main thread rather than doing stuff directly so that I do not touch the same memory from two different threads at the same time. Wait throws any exceptions or provides any return values from the other thread.
The fc library comes with a cooperatively multitasking scheduler that is entirely lockfree and enables blocking calls that don't actually block the thread, instead wait() allows the calling thread to run other tasks (kicked off by async).
-
Isn't that what .wait() is doing? It blocks the main thread to wait for the other thread to complete.
Actually this is the input thread being blocked waiting for the main thread to finish its work. So the main thread isn't what is blocking.