c++ - Is std::async guaranteed to be called for functions returning void? -


i've wrote following code test std::async() on functions returning void gcc 4.8.2 on ubuntu.

#include <future> #include <iostream>  void functiontbc() {     std::cerr << "print here\n"; }  int main(void) { #ifdef use_async     auto = std::async(std::launch::async, functiontbc); #else     auto = std::async(std::launch::deferred, functiontbc); #endif     //i.get();     return 0; } 

if i.get(); uncommented, message "print here" exists; however, if i.get(); commented out, "print here" exists if , if use_async defined (that is, std::launch::async leads message printed out while std::launch::deferred never).

is guaranteed behavior? what's correct way ensure asynchronous call returning void executed?

std::launch::deferred means "do not run until .wait() or .get()".

as never .get() or .wait()ed, never ran.

void has nothing this.

for std::launch::async, standard states returned future's destructor (~future) block until task complete (ie, has implicit .wait()). violated msvc on purpose, because disagreed design decision, , fighting change standard: in practice, means cannot rely on behavior @ std::launch::async returned future if want future-proof code.

without implicit wait in ~future, indeterminate if invoked function when main exited. either have happened, or not. possibly invoke ub having still-active threads @ end of main.

you may wonder use deferred has: can use queue computation lazy evaluation.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -