emscripten-core / emscripten-core/emscripten

Call my callback function in other thread with proxying.h

Open
#22,307 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

When I call my "**cb**" pointer function in the main thread, my "**cb_js**" function on the JS side is triggered.
However, when I do this in a separate thread, it does not work.

**C++:**
```
void (*cb)(int a);

extern "C" EMSCRIPTEN_KEEPALIVE void init_cb(void(*cb_js)(int a)){
cb = cb_js;
}

void process(int a){
cb(a);
}

extern "C" EMSCRIPTEN_KEEPALIVE void Start(){
process(5);
}
```

**JS:**
```
function cb_js(a){
console.log(a);
}

let ptr_cb = Module.addFunction(cb_js, "vi");
Module.ccall("init_cb", "void", ["number"], [ptr_cb]);
Module.ccall("Start", "void", [], []);
```

The codes above are working.
However, when **I want to access my callback function in a other thread** as follows, I cannot access it.
I think this is possible with "**proxying.h**".

**C++:**
```
extern "C" EMSCRIPTEN_KEEPALIVE void Start(){
std::thread([](){
process(5);
}).join();
```

}

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.