emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Call my callback function in other thread with proxying.h

Open moondayyy opened this issue 7 months ago • 1 comments

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();

}

moondayyy avatar Aug 01 '24 07:08 moondayyy