Why is code running async if all we are doing is wating for it to complete?

Started by HardFork, February 23, 2014, 11:32:50 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bytemaster

Quote from: 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.

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.
For the latest updates checkout my blog: [url="http://bytemaster.bitshares.org"]http://bytemaster.bitshares.org[/url]
Anything said on these forums does not constitute an intent to create a legal obligation or contract between myself and anyone else.   These are merely my opinions and I reserve the right to change them at any time.

bytemaster

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).
For the latest updates checkout my blog: [url="http://bytemaster.bitshares.org"]http://bytemaster.bitshares.org[/url]
Anything said on these forums does not constitute an intent to create a legal obligation or contract between myself and anyone else.   These are merely my opinions and I reserve the right to change them at any time.

HardFork

Isn't that what .wait() is doing?  It blocks the main thread to wait for the other thread to complete.

Troglodactyl

Because blocking the main thread is a wicked thing to do?

EDIT: Ambiguated my punctuation.

HardFork

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 ...