Passing byte array from non-main-thread C++ to main-thread JS

63 views
Skip to first unread message

Hanns Holger Rutz

unread,
Dec 31, 2020, 7:21:59 AM12/31/20
to emscripte...@googlegroups.com
Hi there,

I wonder what is the best way to pass a byte array from non-main-thread
C++ to main-thread JS. I'm hesitating to call MAIN_THREAD_SYNC_EM_ASM
because I don't want to block the C++ thread. So the next two options
are EM_ASM and MAIN_THREAD_ASYNC_EM_ASM. In the asynchronous case,
obviously I would have to malloc in C++ and free in JS. Is that
possible? In the EM_ASM case, JS doesn't have access to any regular
objects from the main JS environment. So I would have to copy the data
and defer to the main JS thread? I suppose there is one worker.js that I
would have to listen to from main, and use postMessage from within EM_ASM?

Also the data is bounded by 8K, could I perhaps use a different
approach, like use a constant size array direct passing somehow?

best, .h.h.

OpenPGP_signature

Sam Clegg

unread,
Jan 6, 2021, 7:38:37 AM1/6/21
to emscripte...@googlegroups.com
I think MAIN_THREAD_ASYNC_EM_ASM is what you want.     malloc and free work both in C++ and in JS just the same and across threads just fine.

Alternatively you could call the C++ function on the main thread using `emscripten_async_run_in_main_runtime_thread` and then once that function is running on the main thread call out ot EM_ASM or other JS library function as needed.

--
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/52f2a4ac-2baa-8d90-1486-c5adc57873f4%40sciss.de.

Hanns Holger Rutz

unread,
Jan 8, 2021, 7:32:30 AM1/8/21
to 'Sam Clegg' via emscripten-discuss
hi sam,

thanks for your thought; in the end I sticked to MAIN_THREAD_EM_ASM,
because it meant I could just pass on a stack allocated structure. so
far there wasn't any perceivable performance problem that would require
me to switch to asynchronous invocation.

best, .h.h.
> <mailto:emscripten-discuss%2Bunsu...@googlegroups.com>.
> <https://groups.google.com/d/msgid/emscripten-discuss/52f2a4ac-2baa-8d90-1486-c5adc57873f4%40sciss.de>.
>
> --
> 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
> <mailto:emscripten-disc...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/emscripten-discuss/CAL_va2-SE0rq8c5Sok4T6VPiohczkJ%3D4%3DOwoQfqkqskZOVj%3Dmw%40mail.gmail.com
> <https://groups.google.com/d/msgid/emscripten-discuss/CAL_va2-SE0rq8c5Sok4T6VPiohczkJ%3D4%3DOwoQfqkqskZOVj%3Dmw%40mail.gmail.com?utm_medium=email&utm_source=footer>.

OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages