c语⾔调⽤js,c–通过事件发出调⽤JS函数
您不需要使⽤uv_queue_work,但LibUV的线程库的⼀部分是uv_async_ *⽅法,这是您在这种情况下理想的⽅法.初始化辅助线程时,还要执⾏uv_async_init以创建共享异步数据结构.此函数也通过回调调⽤,该回调将在您的其他线程发送消息时运⾏.该回调是您调⽤JS代码来触发事件的地⽅.
这⾥有⼀些半假的代码作为例⼦:
在你的线程init函数中从JS调⽤,带有⼀个回调arg:
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<:function> js_callback = args[0].As();
// 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
}
在线程中:
async_data.data = (void*) // Some
uv_async_send(&async_data);
在线程回调中:
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.
js arguments
v8::Local count = unt);
v8::Local<:value> argv[] = {
count
};
js_callback->Call(NanNull(), 1, argv);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。