Callback from another thread in NodeJS Internal extension

I am new to nodeJS and node. I am writing my own extension for node js that will receive a callback for the OnEvent virtual function (param1, param2, param3). 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; } 

I need to redirect these callback parameters to a script server using m_EventCallback. The bool OnEvent function is called from another thread.

I tried using uv_async_send but could not succeed.

Any help or guidance would be appreciated.

+4
source share
2 answers

Using uv_async_send is the correct way:

  • call uv_async_init in the main thread.
  • then call uv_async_send from your worker.
  • Do not forget to return uv_close to the main one.

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

+5
source

Perhaps uv_callback would be an option.

With it, we can call functions for other threads.

It can handle non-coalescing calls.

We can even get the result in the caller thread on another function asynchronously. eg:

In the called thread:

 uv_callback_t send_data; void * on_data(uv_callback_t *handle, void *data) { int result = do_something(data); free(data); return (void*)result; } uv_callback_init(loop, &send_data, on_data, UV_DEFAULT); 

In the calling thread:

 uv_callback_t result_cb; void * on_result(uv_callback_t *handle, void *result) { printf("The result is %d\n", (int)result); } uv_callback_init(loop, &result_cb, on_result, UV_DEFAULT); uv_callback_fire(&send_data, data, &result_cb); 
+1
source

All Articles