BitShares Forum

Main => Technical Support => Topic started by: HardFork on February 23, 2014, 11:32:50 pm

Title: Why is code running async if all we are doing is wating for it to complete?
Post 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 ...
Title: Re: Why is code running async if all we are doing is wating for it to complete?
Post by: Troglodactyl on February 24, 2014, 12:32:15 am
Because blocking the main thread is a wicked thing to do?

EDIT: Ambiguated my punctuation.
Title: Re: Why is code running async if all we are doing is wating for it to complete?
Post by: HardFork on February 24, 2014, 01:01:36 am
Isn't that what .wait() is doing?  It blocks the main thread to wait for the other thread to complete.
Title: Re: Why is code running async if all we are doing is wating for it to complete?
Post by: bytemaster on February 25, 2014, 06:39:02 am
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).
Title: Re: Why is code running async if all we are doing is wating for it to complete?
Post by: bytemaster on February 25, 2014, 06:40:40 am
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.