从 emscripten-threads 访问 js-context
access js-context from emscripten-threads
我喜欢从一个线程调用一些global-context-js-function(使用emscripten编译),但是global-Js-context是不可访问的(ReferenceError: jsFunc is not defined)。我可以想象这个问题的主要原因是WebWorker(用于emscripten中的线程)没有访问global-js-context(例如window)。
有没有办法从 cpp-emscripten-thread 访问 global-js-context?或者至少在线程和 global-js-context 之间使用某种事件总线?有什么方法可以沟通吗?
现在,我正在从 js 轮询 cpp 变量,该变量由线程更改。希望有更好的方法。
您可以在下面找到此问题的示例。
也许我忽略了它的一些 emscripten 功能?
谢谢,
本尼迪克特。
#include <pthread.h>
#include <emscripten.h>
EM_JS(void, jsFunc, (int msg), {
// some function in global js context
myCustomLog(msg);
});
void* perform_work(void*) {
printf("in thread \n");
// this call of some global scope js function fails
// ReferenceError: jsFunc is not defined
jsFunc(1);
}
int main(int argc, char** argv) {
pthread_t thread;
printf("in main\n");
// this call of some global scope js function works fine
jsFunc(0);
pthread_create(&thread, NULL, perform_work, NULL);
}
构建:
emcc -o test.html -s USE_PTHREADS=1 ../main.cpp
我找到方法了。
MAIN_THREAD_ASYNC_EM_ASM('myCustomLog(3);');
myCustomLog(3);
是主浏览器线程中的一个函数
我喜欢从一个线程调用一些global-context-js-function(使用emscripten编译),但是global-Js-context是不可访问的(ReferenceError: jsFunc is not defined)。我可以想象这个问题的主要原因是WebWorker(用于emscripten中的线程)没有访问global-js-context(例如window)。
有没有办法从 cpp-emscripten-thread 访问 global-js-context?或者至少在线程和 global-js-context 之间使用某种事件总线?有什么方法可以沟通吗?
现在,我正在从 js 轮询 cpp 变量,该变量由线程更改。希望有更好的方法。
您可以在下面找到此问题的示例。
也许我忽略了它的一些 emscripten 功能?
谢谢,
本尼迪克特。
#include <pthread.h>
#include <emscripten.h>
EM_JS(void, jsFunc, (int msg), {
// some function in global js context
myCustomLog(msg);
});
void* perform_work(void*) {
printf("in thread \n");
// this call of some global scope js function fails
// ReferenceError: jsFunc is not defined
jsFunc(1);
}
int main(int argc, char** argv) {
pthread_t thread;
printf("in main\n");
// this call of some global scope js function works fine
jsFunc(0);
pthread_create(&thread, NULL, perform_work, NULL);
}
构建:
emcc -o test.html -s USE_PTHREADS=1 ../main.cpp
我找到方法了。
MAIN_THREAD_ASYNC_EM_ASM('myCustomLog(3);');
myCustomLog(3);
是主浏览器线程中的一个函数