Launch emscripten artifact with pthreads in Javascript

237 views
Skip to first unread message

Alexandr Gribkov

unread,
Jul 26, 2022, 4:15:31 AM7/26/22
to emscripten-discuss
```
Emscripten: 3.1.13
EMBIND
CMAKE linking/compiling
set(CMAKE_CXX_FLAGS "-O0 -lembind -pthread -g3 -s ALLOW_MEMORY_GROWTH=1 -s USE_PTHREADS=1")
${PROJECT_NAME}
    "-s WASM=1"
    "-s EXPORT_ES6=1"
    "-s ALLOW_MEMORY_GROWTH=1"
    "-s ENVIRONMENT=web,worker"
    "-s PTHREAD_POOL_SIZE='navigator.hardwareConcurrency'"
    "-s MODULARIZE=1"
```

On C++ side guys are using code with pthreads
Emscripten produces the following files
```
wasm_test.js
wasm_test.wasm
wasm_test.worker.js
```
on the frontend side, we are using the following code to launch the binary within web worker produced by emscripten
```
    const worker = new Worker('%PUBLIC_URL%/test/test_web.worker.js');
    const memory = getSharedMemory()
    worker.onmessage = (data) => {
      if (data.data.cmd === 'loaded') {
        worker.postMessage({ cmd: 'run', threadInfoStruct: ??? })
      }
    };
    worker.onerror = console.log.bind(null, 'on error ');
    fetch('/test/test_web.wasm').then(data => data.arrayBuffer()).then(bytes => {
      const mod = WebAssembly.compile(bytes);
      mod.then((wasmModule) => {
        worker.postMessage({
          cmd: 'load',
          wasmModule,
          urlOrBlob: '%PUBLIC_URL%/test/test_web.js',
          wasmMemory: memory
        })
      })
    });
```

Worker firing the `loaded` event but I can't figure out how to launch the run event/method
What is the right payload for the following event `worker.postMessage({ cmd: 'run', ??? })`???
How to run any C++ bound method?

Sam Clegg

unread,
Jul 26, 2022, 1:20:08 PM7/26/22
to emscripte...@googlegroups.com
Answered on https://github.com/emscripten-core/emscripten/issues/17523#issuecomment-1195755927

TLDR: You should let emscripten handle the creation of new workers/pthreads.  No need to do any of that yourself, just call `pthread_create`.

--
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/796df541-42c8-40a3-b53e-d62c79146593n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages