Open SoftwareMechanic opened 2 months ago
Emscripten will only only create new workers when there are no unused workers in the pool.
Workers are returned to the "unused" pool each time a C++ thread exits.
If you are seeing the pool continue to grow then that means that your C++ threads probably are not exiting correctly and emscripten things that they are still running. Perhaps there is some issue with the way C++ threads are built on top of pthreads? Perhaps libc++ is pooling pthreads at some level?
I wonder if you could share a simple example of a program that continues to grow the worker pool even though the number of active C++ threads is not going up?
thank you for the response @sbc100 !
I found that even with the following code, at some point, will try to create workers:
std::cout << "Test trheads!" << std::endl
while (true) {
std::thread test1(func);
test1.detach();
test1.~thread();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
void func()
{
std::cout << "I'm a thread!\n";
}
I can see the output printed, but after some iterations, it fails because of the same error.
shouldn't detach
free the worker?
I tried also not allowing the memory to grow but it doesn't change the result.
I am sorry if this is a noob error, I am new to Emscripten, so please bear with me.
EDIT:
also by setting -sPTHREAD_POOL_SIZE_STRICT=0
I face the same behaviour, but errors will not be printed
EDIT2:
Using std::thread::join()
instead of detach
just works with that simple example, and every thread created has the same ID.
if you need more info don't hesitate to ask!
You can see this explanation, and I believe you will get a good solution.
V.3.1.39
I am compiling and running a program which use 3rd party C++ library.
the 3rd party library uses multithreading. theoretically uses the number of processors as maximum number of threads, (that number is an input I give using
std::thread::hardware_concurrency();
)but when this program is compiled with Emscripten and run in the browser, seems that it creates/uses a different web worker for each iteration, instead of "reusing" already created threads/workers, as it should.
this is where multithreading starts
init_future_ = std::async(std::launch::async, [this]() { process_concurrently(); });
:this is processConcurrently function
I think this could be somehow related to https://github.com/emscripten-core/emscripten/issues/8201
ps. I tried setting THREAD_POOL_SIZE to 490 (just as a test) and the iteration goes for more or less 490 objects.
this is an example on how I use the iterator
errors:
any tip will be much appreciated!!