Async calls - what is the "main thread"?

28 views
Skip to first unread message

Ilya Kantor

unread,
Feb 9, 2023, 3:18:37 AM2/9/23
to emscripten-discuss
Hello,

I'm reading the docs and getting confused, what the "Main thread" is?

Sometimes it's referred to as the "Main browser thread", the term appears in macroses such as MAIN_THREAD_EM_ASM.

I see two possible explanations there, please tell me which one is correct?

1) The "main thread" is the main browser page thread that loaded WASM webworker, the one with the "window" object. It has access to DOM etc. So we can use it to work from DOM from C.


But it doesn't work for me like that, and I don't see any `postMessage` calls in JS,  how does Emscripten implement the interaction with the main page?

2) The "main thread" is related to pthread usage: it's the all-parenting pthread, when we have multiple pthreads running. Otherwise, if we're not using pthreads, then it's irrelevant, the main thread is the only thread anyway.

In other words, the "main thread" is a pure pthreads concept, the all-parenting thread.

3) ...Can it be sometimes 1) and sometimes 2)? =)

Please help me to figure out.

Thank you!

Kind regards,
Ilya Kantor

Thomas Lively

unread,
Feb 9, 2023, 1:51:56 PM2/9/23
to emscripte...@googlegroups.com
> 1) The "main thread" is the main browser page thread that loaded WASM webworker, the one with the "window" object. It has access to DOM etc. So we can use it to work from DOM from C.

This is correct. There is a "proxying" system in Emscripten's system libraries that handles sending work between threads, but ultimately it is implemented on top of postMessage. I'm not sure how you looked for postMessage calls, but if you build with pthreads enabled, they should be in there somewhere.

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/029031da-a7f9-47dc-8db0-91a52672e9aan%40googlegroups.com.

Ilya Kantor

unread,
Feb 9, 2023, 2:30:47 PM2/9/23
to emscripte...@googlegroups.com
Hi,

I tried to build with threads, but I get this error in Chrome: "Uncaught ReferenceError: SharedArrayBuffer is not defined".

It seems that there's no support in Chrome?

Does that mean that Emscripten can't run with USE_PTHREADS in Chrome? And so the MAIN_THREAD_* functionality is unavailable?

Kind regards,
Ilya Kantor


Sam Clegg

unread,
Feb 9, 2023, 2:37:47 PM2/9/23
to emscripte...@googlegroups.com
The term "main thread" can be ambiguous and confusing I'm afraid.

In emscripten we disambiguate we have two concepts of `main` thread.

1. The main browser thread (i.e. not a worker)
2. The main emscripten runtime thread (the thread where the emscripten module get first loaded).

Normally on the web these two are the same thing since the emscripten module gets instantiated on the main browser thread.  However if you load the emscripten module on a worker, then there will be no main browser thread, or at leat no main browser thread that is accessible to emscripten.

When we say "proxy to main thread" or "MAIN_THREAD_EM_ASM" we are always talking about the main runtime thread, since that is the only thing that is guaranteed to exist.

There are many APIs which won't work if your main runtime thread is not also the main browser thread (e.g anything related to the DOM).

Note: I myself have been confused about these concepts in the past, and my knowledge could still have gaps but that is my understanding today.

cheers,
sam



--

Ilya Kantor

unread,
Feb 9, 2023, 2:52:25 PM2/9/23
to emscripte...@googlegroups.com
Hello,

I thought, I was supposed to load it only in a worker... That's because of examples, I guess.

Can I load it in the main browser page without side-effects? (assuming that there's no blocking stuff )? :)

Kind regards,
Ilya Kantor

Sam Clegg

unread,
Feb 9, 2023, 5:24:55 PM2/9/23
to emscripte...@googlegroups.com
On Thu, Feb 9, 2023 at 11:52 AM Ilya Kantor <ili...@gmail.com> wrote:
Hello,

I thought, I was supposed to load it only in a worker... That's because of examples, I guess.

Which examples are you referring to?    Pretty much all the emscripten sample code and test code that I know of loads the emscripten JS file on the main thread along with the html. 



Can I load it in the main browser page without side-effects? (assuming that there's no blocking stuff )? :)

Emscripten takes care to avoid blocking the main browser thread as much as possible.   Its still possible to do though, and its up to you to split up your main loop.  This is true both with and without threads.  There is more information on this here: https://emscripten.org/docs/porting/emscripten-runtime-environment.html#browser-main-loop

 
Reply all
Reply to author
Forward
0 new messages