Skip to content

Find a way to inform users about "pthreads don't start while main is blocked" issue #11554

Closed

Description

@sbc100

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?

Activity

  1. sbc100 commented on Jul 6, 2020

    @sbc100
    CollaboratorAuthor

    An FAQ entry might also be good.

  2. kripken commented on Jul 6, 2020

    @kripken
    Member

    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.

  3. RReverser commented on Sep 25, 2020

    @RReverser
    Collaborator

    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?

  4. kripken commented on Oct 2, 2020

    @kripken
    Member

    @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.

  5. RReverser commented on Oct 2, 2020

    @RReverser
    Collaborator

    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.

  6. kripken commented on Oct 3, 2020

    @kripken
    Member

    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.)

  7. RReverser commented on Oct 3, 2020

    @RReverser
    Collaborator

    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?

  8. kripken commented on Oct 3, 2020

    @kripken
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions