Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Callback from different thread in NodeJS Native extension

Callback from different thread in NodeJS Native extension

Problem

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 argv[3] = {
            Local::New(Integer::New(1)),
            Local::New(String::New("parameter 1")),
            Local::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;
}

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

I tried using uv_async_send but couldn't succeed.

Any help or guidance will be appreciated.

Problem courtesy of: Rahul Shukla

Solution

Using uv_async_send is the correct way:

  • call uv_async_init on the 'main' thread.
  • then call uv_async_send from your worker.
  • don't forget uv_close back on main.

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

Solution courtesy of: laktak

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Callback from different thread in NodeJS Native extension

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×