You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Find a way to inform users about "pthreads don't start while main is blocked" issue #11554
This issue comes up for almost all user who try to use pthreads for the first time. See #11543.
This is because it effects the most basic hello world pthreads programs.
I think the ideal solution would be assertion that fires when then main thread tries to block on a pthread that has not started. For example, calling pthread_join on a thread that hasn't started is probably the most common way this manifests. I think we should be able to assert here. For other types of blocking such as mutex/condition waits this might not be possible.
To cast a wider net we could log a warning to the console when new workers are started from the main function?
Good idea. I think we already warn about blocking in the console, but not this particular issue. Perhaps the pthread_create call could at least log something like creating pthread (this will not be available until the next event loop iteration - look for a later logging about that) and when that iteration happens and the pthread is created, to log the followup.
One idea I briefly played with was creating one thread at the start of the program (it could be Emscripten's runtime dependency), and then instead of creating new Workers directly, asking it to create more threads via a SharedArrayBuffer channel.
This way, it's always possible to do pthread_create followed by blocking of the current thread even without a pre-existing pool of Workers - only one helper Worker is necessary.
Perhaps Emscripten could implement this as an alternative setting?
On one hand, creating workers is somewhat expensive as they have to be fetched & spawned. In this case a pool option might be helpful to squeeze a bit more of performance, as it creates all of the Workers at the startup in parallel with other runtime deps.
On the other hand, perhaps with correctly set caching it doesn't matter that much.
Thinking some more on the idea of creating a thread for thread creation, it's similar to what PROXY_TO_PTHREAD does, but also allows keeping main() on the main browser thread. That's an advantage in one way, but it will also be a disadvantage in responsiveness overall, since PROXY_TO_PTHREAD moves all non-essential things off the main thread. So I think it would be best to focus on promoting PROXY_TO_PTHREAD and improving its ease of use, rather than adding another mode.
(That's all separate from the issue in the title - I do still think it's worth finding a way to warn about not being able to create a pthread in non-PROXY_TO_PTHREAD mode.)
it's similar to what PROXY_TO_PTHREAD does, but also allows keeping main() on the main browser thread
Yeah that sounds like a good summary. I found this mode simpler at least when used outside of Emscripten, because one doesn't need to worry at least about all the APIs that are only available on the main thread, but you're right that since PROXY_TO_PTHREAD exists, we can just route users there. Will it proxy a thread creation though or would it similarly block the "main" thread?
Yes, it proxies thread creation. At least, when possible - when you want to transfer a Canvas, it has to use postMessage. I see the docs don't mention this, I'll fix that.
Activity
sbc100 commentedon Jul 6, 2020
An FAQ entry might also be good.
kripken commentedon Jul 6, 2020
Good idea. I think we already warn about blocking in the console, but not this particular issue. Perhaps the
pthread_createcall could at least log something likecreating pthread (this will not be available until the next event loop iteration - look for a later logging about that)and when that iteration happens and the pthread is created, to log the followup.RReverser commentedon Sep 25, 2020
One idea I briefly played with was creating one thread at the start of the program (it could be Emscripten's runtime dependency), and then instead of creating new Workers directly, asking it to create more threads via a SharedArrayBuffer channel.
This way, it's always possible to do
pthread_createfollowed by blocking of the current thread even without a pre-existing pool of Workers - only one helper Worker is necessary.Perhaps Emscripten could implement this as an alternative setting?
kripken commentedon Oct 2, 2020
@RReverser Interesting idea!
I think it could work. It may be best to just do that all the time, to avoid an extra setting and the complexity of different code paths for this.
RReverser commentedon Oct 2, 2020
Hm, maybe...
On one hand, creating workers is somewhat expensive as they have to be fetched & spawned. In this case a pool option might be helpful to squeeze a bit more of performance, as it creates all of the Workers at the startup in parallel with other runtime deps.
On the other hand, perhaps with correctly set caching it doesn't matter that much.
kripken commentedon Oct 3, 2020
Thinking some more on the idea of creating a thread for thread creation, it's similar to what
PROXY_TO_PTHREADdoes, but also allows keepingmain()on the main browser thread. That's an advantage in one way, but it will also be a disadvantage in responsiveness overall, sincePROXY_TO_PTHREADmoves all non-essential things off the main thread. So I think it would be best to focus on promotingPROXY_TO_PTHREADand improving its ease of use, rather than adding another mode.(That's all separate from the issue in the title - I do still think it's worth finding a way to warn about not being able to create a pthread in non-
PROXY_TO_PTHREADmode.)RReverser commentedon Oct 3, 2020
Yeah that sounds like a good summary. I found this mode simpler at least when used outside of Emscripten, because one doesn't need to worry at least about all the APIs that are only available on the main thread, but you're right that since
PROXY_TO_PTHREADexists, we can just route users there. Will it proxy a thread creation though or would it similarly block the "main" thread?kripken commentedon Oct 3, 2020
Yes, it proxies thread creation. At least, when possible - when you want to transfer a Canvas, it has to use postMessage. I see the docs don't mention this, I'll fix that.
Document and test that PROXY_TO_PTHREAD fixes the pthread_create issue (
Make it easier to detect thread pool exhaustion
Make it easier to detect thread pool exhaustion
Make it easier to detect thread pool exhaustion
Make it easier to detect thread pool exhaustion (#13591)
Disable concurrency in jxl{load,save}