Sep*_*ehr 5 c++ multithreading node.js node-modules
我不确定如何通过在单独的C++线程中使用事件发出来调用Node的主线程中的Node/v8函数.如何在C++线程中发出事件?
在考虑NanAsyncWorker或uv_queue_work之前:我不想以异步方式调用C++函数.我想做的恰恰相反,通过发出事件从C++调用Javascript函数.
您不需要使用uv_queue_work,但 LibUV 线程库的一部分是uv_async_*方法,在这种情况下您最好拥有这些方法。当您初始化辅助线程时,您还需要uv_async_init创建一个共享async数据结构。该函数还会通过回调来调用,只要其他线程发送消息,该回调就会运行。您可以在该回调中调用 JS 代码来触发事件。
这是一些半伪代码作为示例:
在从 JS 调用的线程 init 函数中,使用单个回调参数:
void ThreadInit(const v8::Arguments &args){
// This is just an example, this should be saved somewhere that
// some_callback will be able to access it.
v8::Persistent<v8::Function> js_callback = args[0].As<Function>();
// And save this where you'll be able to call uv_close on it.
uv_async_t async_data;
uv_async_init(uv_default_loop(), &async_data, some_callback);
// initialize the thread and pass it async_data
}
Run Code Online (Sandbox Code Playgroud)
在线程中:
async_data.data = (void*) // Some data structure...
uv_async_send(&async_data);
Run Code Online (Sandbox Code Playgroud)
在线程回调中:
void some_callback(uv_async_t *async_data){
// Note that this depending on the data, you could easily get thread-safety issues
// here, so keep in mind that you should follow standard processes here.
void* data = async_data->data;
// Process that data however you need to in order to create a JS value, e.g.
// Using NanNew because it is more readable than standard V8.
v8::Local<Number> count = NanNew<Number>(data.count);
v8::Local<v8::Value> argv[] = {
count
};
js_callback->Call(NanNull(), 1, argv);
}
Run Code Online (Sandbox Code Playgroud)