从NodeJS本机扩展中的不同线程回调 [英] Callback from different thread in NodeJS Native extension

查看:118
本文介绍了从NodeJS本机扩展中的不同线程回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是nodeJS和节点扩展的新手。我为节点js编写一个本地扩展,它将接收一个虚函数OnEvent(param1,param2,param3)上的回调。代码如下:

I am newbie in nodeJS and node extension. I m writing a native extension for node js which will receives a callback on an virtual function OnEvent(param1, param2, param3). The code follows:

bool MyExt::OnEvent(int eventType, string param1, string param2)
{
    printf("MyExt:: onevent___ \n");
    {
        //// Crashes here, but if I use Locker, it get stuck!!!!!!
        //Locker l;
        Local<Value> argv[3] = {
            Local<Value>::New(Integer::New(1)),
            Local<Value>::New(String::New("parameter 1")),
            Local<String>::New(String::New("parameter 2"))
        };

        TryCatch try_catch;
        //// I need to call this
        m_EventCallback->Call(Context::GetCurrent()->Global(), 3, argv);
        if (try_catch.HasCaught()){
                printf("Callback is Exception()  \n");
        }
        printf("Callback is IsCallable() \n");
    }
    return true;
}



我需要使用m_EventCallback将此回调参数转发到服务器脚本。 bool OnEvent是从不同的线程调用的。

I need to forward this callback parameters to the server script using m_EventCallback.The function bool OnEvent is called from a different thread.

我尝试使用uv_async_send但不能成功。

I tried using uv_async_send but couldn't succeed.

任何帮助或指导都会感激。

Any help or guidance will be appreciated.

推荐答案

使用uv_async_send是正确的方式:

Using uv_async_send is the correct way:


  • 在主线程上调用uv_async_init。

  • 然后调用uv_async_send

http://nikhilm.github.com/uvbook/threads.html

这篇关于从NodeJS本机扩展中的不同线程回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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