来自多线程C ++插件的回调NodeJS Javascript函数 [英] Callback NodeJS Javascript function from multithreaded C++ addon

查看:94
本文介绍了来自多线程C ++插件的回调NodeJS Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多线程C ++附加组件,它可以进行一些后台处理,因此需要定期回调到我在NodeJS服务器中编写的Javascript函数.

I have a multithreaded C++ addon that does some background processing and I need to have it periodically callback to a Javascript function that I wrote in my NodeJS server.

我知道这涉及使用uv_async_send(),因为它需要在主线程中执行,但是到目前为止,我还无法弄清楚该怎么做.

I understand that this involves using uv_async_send(), since it needs to get executed in the main thread, but thus far I haven't been able to figure out how to do it.

有没有一个我想念的简单例子?

Is there a simple example out there that I've missed?

推荐答案

最后,一旦我了解uv_ *函数的作用,这并不太困难:

Finally, this was not too difficult once I understood what the the uv_* functions do:

1)在附加组件中公开一个函数,以允许Node设置将定期调用的Javascript cb

Callback* cbPeriodic; // keep cbPeriodic somewhere
NAN_METHOD(setPeriodicCb) {
    cbPeriodic = new Callback(info[0].As<Function>());
    //...
}

2)使用uv_async_t实例和将在我们的工作线程调用uv_async_send()

2) Init UV with an uv_async_t instance and a function that will be executed in the main thread by UV when our worker thread calls uv_async_send()

uv_async_t async; // keep this instance around for as long as we might need to do the periodic callback
uv_loop_t* loop = uv_default_loop();
uv_async_init(loop, &async, asyncmsg);

void asyncmsg(uv_async_t* handle) {
  // Called by UV in main thread after our worker thread calls uv_async_send()
  //    I.e. it's safe to callback to the CB we defined in node!
  Nan::HandleScope scope;
  v8::Isolate* isolate = v8::Isolate::GetCurrent();
  Local<Value> argv[] = { v8::String::NewFromUtf8(isolate, "Hello world") };
  cbPeriodic->Call(1, argv);
}

3)每当需要执行定期回调时,从工作线程中调用uv_async_send,并在上面传递异步实例

3) Call uv_async_send from a worker thread, passing our async instance above, whenever we need to do the periodic callback

uv_async_send(&async);

4)最后,当我们不再需要再次执行回调时,请进行清理:

uv_close((uv_handle_t*) &async, NULL);


附录:


Addendum:

自从我写了这个答案以来,我遇到了其他问题,最后学到了一些有关libuv的课程.为了完整起见,您应该了解:

Since I wrote this answer I've run into other issues and finally learned some lessons about libuv. For completeness, you should understand:

  • 除了uv_async_send之外,所有 libuv函数可能 从主循环线程中调用! (我已经看到它提到其他线程不是线程安全的,但这在声明中太弱了.)即使,例如,必须从主循环线程中调用uv_async_inituv_close.

  • Aside from uv_async_send, all libuv functions may only be called from the the main loop thread! (I had seen it mentioned that the others were not thread-safe, but this is too weak of a statement.) Even, for example, uv_async_init and uv_close must be called from the main loop thread.

如果动态分配了uv_async_t实例,请注意,直到uv_close进行回调以使您知道这样做是安全的,您才可以释放内存.

If your uv_async_t instance is dynamically allocated, note that you may not free up memory until uv_close makes its callback to let you know that it is safe to do so.

即:

auto async = new uv_async_t();
...
uv_close((uv_handle_t*)async, [](uv_handle_t* handle) {
    delete handle;
});

这篇关于来自多线程C ++插件的回调NodeJS Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆