Author Topic: Why is code running async if all we are doing is wating for it to complete?  (Read 1935 times)

0 Members and 1 Guest are viewing this topic.

Offline bytemaster

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: http://bytemaster.bitshares.org
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.

Offline 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: http://bytemaster.bitshares.org
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.

Offline HardFork

  • Jr. Member
  • **
  • Posts: 26
    • View Profile
Isn't that what .wait() is doing?  It blocks the main thread to wait for the other thread to complete.

Offline Troglodactyl

  • Hero Member
  • *****
  • Posts: 960
    • View Profile
Because blocking the main thread is a wicked thing to do?

EDIT: Ambiguated my punctuation.
« Last Edit: February 24, 2014, 02:55:16 am by Troglodactyl »

Offline HardFork

  • Jr. Member
  • **
  • Posts: 26
    • View Profile
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 ...