-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
std::async will hang if no threads are available #8988
Comments
May it be related to
or #8325 ? |
That bug was a little different, since the guy was trying to run the whole Emscripten runtime in a worker. |
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant. |
For further readers; note that Alternatively, you could try to use #include <thread>
#include <future>
#include <iostream>
int main() {
// works
std::thread([] {
std::cout << "Thread ID: " << std::this_thread::get_id() << std::endl;
}).join();
// works
std::async([] {
std::cout << "Thread ID: " << std::this_thread::get_id() << std::endl;
});
return 0;
} Compile with: emcc test.cpp -std=c++11 -s ASSERTIONS=0 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=1 -o test.html (Tested with Emscripten 2.0.1 without problems) |
So the issue described is really about thread spawn with So for this example setting |
Ah I see, in that case this SO post is relevant here. Try this: #include <thread>
#include <future>
#include <iostream>
std::future<void> future;
int main() {
// works
std::thread([] {
std::cout << "Thread ID: " << std::this_thread::get_id() << std::endl;
}).detach();
// works
auto f = std::async([] {
std::cout << "Thread ID: " << std::this_thread::get_id() << std::endl;
});
// transfer the future's shared state to a longer-lived future
future = std::move(f);
return 0;
} Compile with: emcc test.cpp -std=c++11 -s USE_PTHREADS=1 -o test.html (Tested with Emscripten 2.0.3 without problems) |
Ah, thanks for that. I'm not using std::async with Emscirpten any more, but this may come in handy in the future. |
@VirtualTim This is kind of related to this issue: I've been doing some tests in https://github.com/OlivierSohn/async-webassembly and concluded that using explicit std::thread(s) to implement parallelism won't work with WebAssembly / emscriptem. Using std::thread, I observed deadlocks when more than 3 threads are running at the same time. Using std::async, there is no such limitation, I could run as many std::async in parallel as I wanted. |
That's odd, I'm running a bunch (12) of std::threads in parallel without too many issues. |
@VirtualTim I've done some more tests, and I observe the same behaviour with detached threads. I've updated my tests (https://github.com/OlivierSohn/async-webassembly) so that instead of having a deadlock, we log an error when not all tasks were able to run at the same time. The results show that we can start many std::thread(s) in parallel, but they won't be able to run all tasks at the same time. This problem doesn't exist with std::async. So my guess is that for your application you could get a better level of parallelism using std::async, because more tasks could be running at the same time. What do you think? |
Hm, that's interesting. One bug I have, that I never managed to track down, was that sometimes on first start the above demo would hang. And it hung hard enough I couldn't even use the dev tools to see what was going on. But it never showed up in the actual product Maybe this is related? I'm not actively working on this at the moment, the plan is to gauge market sentiment and and browser maturity, but I'll have to look into this when work is started back up. |
No idea why this is different from regular threads.
Compile with:
emcc test.cpp -std=c++11 -s USE_PTHREADS=1 -o test.html
to see the issue.Compile with:
emcc test.cpp -std=c++11 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=2 -o test.html
to make this work as expected.The text was updated successfully, but these errors were encountered: