emscripten-core / emscripten-core/emscripten

Deadlock happens when allocating pool of threads and instantiating wasm in a worker

Open
#15,100 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Below is a sample code using boost thread pool:

```
#include
#include
#include

int task_num = 1;

int main()
{
std::printf("Inside main\n");
boost::asio::thread_pool tp(5);
auto task = [](){
std::stringstream msg;
msg << "Task " << task_num++ << std::endl;
std::cout << msg.str();
};
std::vector> vec;
for (int i=0; i < 5; ++i) {
vec.push_back(boost::asio::post(tp, boost::asio::use_future(task)));
}
for(auto& fut: vec){
printf("Before wait\n");
fut.wait();
}
std::cout << "Completed" << std::endl;
return 0;
}
```

I have observed few cases and they are confusing me:

- Using `-sPTHREAD_POOL_SIZE=16`
- When I instantiate wasm on main thread (basically using generated html by em++) no deadlock happens but I get below warning on console.
`boost.html:1246 Blocking on the main thread is very dangerous`
- When I instantiate wasm on a worker, deadlock happens. I tried to debug and execution stalls after first `wait`. Function given to threads i.e. `task` also get executed but somehow wait is not resolved.

- Using `-sPROXY_TO_PTHREAD`
- Both cases work and no deadlock happens.

What I was excepting is `-sPTHREAD_POOL_SIZE=16` to work also in the case when I am instantiating wasm in a worker since I am pre-allocating required number of threads.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.