emscripten-core / emscripten-core/emscripten
Run ffmpeg multithread wasm in worker failed
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
I try to run a multithread app in a worker, it does not work and no error.However it can run in main thread.
Here is the c code from https://developers.google.com/web/updates/2018/10/wasm-threads?spm=a2c6h.12873639.0.0.65c36f8aqWHCjg.
I want to know if i can run multithread webassembly in worker and how?
```
#include
#include
// Calculate Fibonacci numbers shared function
int fibonacci(int iterations) {
int val = 1;
int last = 0;
if (iterations == 0) {
return 0;
}
for (int i = 1; i < iterations; i++) {
int seq;
seq = val + last;
last = val;
val = seq;
}
return val;
}
// Start function for the background thread
void *bg_func(void *arg) {
int *iter = (void *)arg;
*iter = fibonacci(*iter);
return arg;
}
// Foreground thread and main entry point
int main(int argc, char *argv[]) {
int fg_val = 54;
int bg_val = 42;
pthread_t bg_thread;
// Create the background thread
if (pthread_create(&bg_thread, NULL, bg_func, &bg_val)) {
perror("Thread create failed");
return 1;
}
// Calculate on the foreground thread
fg_val = fibonacci(fg_val);
// Wait for background thread to finish
if (pthread_join(bg_thread, NULL)) {
perror("Thread join failed");
return 2;
}
// Show the result from background and foreground threads
printf("Fib(42) is %d, Fib(6 * 9) is %d\n", bg_val, fg_val);
return 0;
}
```
Then emcc compile to ```emcc -O2 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=2 -o test.js test.c```.
js code
```
let worker = new Worker('worker.js')
worker.postMessage('init')
// worker.js
onmessage = (event) => {
const { data } = event
if (data === 'init') {
importScripts('test.js')
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.