emscripten-core / emscripten-core/emscripten

Can't call JS function added by addFunction from secondary thread

Open
#21,273 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

**Without** using **thread**(worker) the codes were **working** smoothly.
However, when I using threads, I cannot get the pointer address of the `Module.addFunction` function.

**JavaScript:**

```
function is_speech(b) {
console.log(b);
}

let pointerIsSpeech = Module.addFunction(is_speech, "vi");
Module.ccall('InitializeIsSpeechWeb', 'void', ['number'], [pointerIsSpeech]);
```

**C++:**

```
std::thread([&isSpeech](){
isSpeech=Process();
cb_isspech((void*)isSpeech);
}).detach();
```

I'm having the same problem as this one(#16567).
mergeInto was mentioned. I tried, but I cannot access the function I defined (I think because I am inside the worker)

**merge.js:**

```
mergeInto(LibraryManager.library,{
cb_isspech: function (isSpeech){
is_speech(isSpeech); <-- It does not see the callback function I created.
console.log("isSpeech: "+isSpeech);
}
});
```

**How can I use JS callback function in MultiThread?**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.