Using pthreads more quickly than they can be returned to the thread pool #23846
Unanswered
badgermole
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I was going to title this, "Not reusing pthreads", but after putting together a minimal example to demonstrate here, I'm pretty sure that I'm just not doing what's needful in order to allow time for threads to return to the pool. I could use some advice on this point.
Note that I do not need a C/C++ main(), as this is being used in the browser more as a library.
In the example, the embound C++ function, "getVersionAsync", creates a detached pthread to go off and do work. The ID of a faux JS promise is passed to it, which it also passes to the pthread function, which, at completion, calls emscripten_async_run_in_main_runtime_thread() to pass the promise ID, and success status back to the main thread so the promise (if it were real) can be resolved. The example project doesn't really do any fussing with actual JS promises, which is immaterial I think in this.
I have two buttons on the UI - one that you can press to call getVersionAsync once, and one that calls it 8 times in a loop. You can press the one repeatedly, and never see the issue. But from the other button, when getVersionAsync is called iteratively more than the initial number of pthreads in the pool, then it gives the warning that the pool has been exhausted, and it creates more pthreads.
In my real environment, there are several API methods that are backed by pthreads this way that are called asynchronously as we process batches of many files. And so, we quickly see the creation of pthreads go out of control.
After seeing how my example behaves with the Exec-1 button, my guess is that I need to do some management of the pthread use. But I'm not sure what the best practice might be there. The pthreads are all created detached, so there is no join. Everything else seems to be working as desired, it's just that the pthreads are not getting reused.
As an aside, also in my real environment, I have an alternate implementation for environments where window.crossOriginIsolated is false. And in that implementation, I'm managing a queue of API calls to send to web workers pool that I manage.
Beta Was this translation helpful? Give feedback.
All reactions