Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems using std::future, std::launch::async #11959

Closed
zajo opened this issue Aug 18, 2020 · 8 comments
Closed

Problems using std::future, std::launch::async #11959

zajo opened this issue Aug 18, 2020 · 8 comments

Comments

@zajo
Copy link

zajo commented Aug 18, 2020

I'm trying to get this simple test to work (on Node) and it seems to deadlock:

int main()
{
    std::future<int> fut = std::async(std::launch::async, [] { return 42; });
    return fut.get();
}

Is this supported at all? I'm compiling/linking with the following options:

WASM=1
USE_PTHREADS=1
PROXY_TO_PTHREAD=1
DISABLE_EXCEPTION_CATCHING=0
@sbc100
Copy link
Collaborator

sbc100 commented Aug 19, 2020

We have no tests for std::async so I guess its not surprising that its not working yet :( If you would like work on this I think other would be excited to see it happen.

@kleisauke
Copy link
Collaborator

Try setting -s PTHREAD_POOL_SIZE=2, see: #8988.

@zajo
Copy link
Author

zajo commented Aug 19, 2020

Thanks for the suggestion but it did not help. Reading the documentation on PTHREAD_POOL_SIZE, is it needed even under PROXY_TO_PTHREAD?

@kleisauke
Copy link
Collaborator

I tried this test:

#include <future>
#include <iostream>

int main() {
    std::future<int> fut = std::async(std::launch::async, [] {
        return 42;
    });
    std::cout << fut.get() << std::endl;
    return 0;
}

I compiled it with:

emcc test.cpp -std=c++11 -s ASSERTIONS=0 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=1 -o test.js

(note that I've set PTHREAD_POOL_SIZE to 1, which simply works, so perhaps #8988 is outdated)

And ran it with Node.js like this:

node --experimental-wasm-threads -e "require('./test.js')"

This prints the expected 42, although the runtime did not exits (I needed to hit Ctrl + C). Perhaps that's the problem?

@kleisauke
Copy link
Collaborator

... If that's the problem, try setting -s EXIT_RUNTIME=1 to allow the runtime to quit when main() is finished.

(Pressed the comment button too early)

@zajo
Copy link
Author

zajo commented Aug 19, 2020

Thanks, yes that was the problem. In fact, it didn't even need PTHREAD_POOL_SIZE. Is this due to PROXY_TO_THREAD?

@kleisauke
Copy link
Collaborator

I think so. Doing pthread_create() calls within a thread (i.e. -s PROXY_TO_PTHREAD=1) can probably be executed synchronously without having to yield back to the main thread.

@zajo
Copy link
Author

zajo commented Aug 25, 2020

Thanks for the help!

@zajo zajo closed this as completed Aug 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants