Uv_queue_work doesn't run callback_method in node addon (C ++)
I am creating a node addon in C ++ and I want to be able to make callbacks from other threads. To try it I am doing the following test using uv_queue_work and Nan. If I call the Hello function, it should start a new thread for the firstMethod method, and when it ends, call the next "callbackMethod" method on another thread where I would make a Javascript callback. But for some reason, it runs the first method and doesn't run the second.
This is my code.
void Hello(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
v8::Local<v8::Function> callback;
callback = args[0].As<v8::Function>();
ListBaton* baton = new ListBaton();
baton->callback = new Nan::Callback(callback);
uv_work_t* req = new uv_work_t();
req->data = baton;
uv_queue_work(uv_default_loop(), req, firstMethod, callbackMethod);
}
void firstMethod(uv_work_t* req) {
std::cout << "Entering PRE thread" << std::endl;
sleep(1);
std::cout << "Leaving PRE thread" << std::endl;
}
void callbackMethod(uv_work_t* req, int status) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
if(!isolate)
{
std::cout << "Isolate was null" << std::endl;
isolate = v8::Isolate::New();
isolate->Enter();
}
ListBaton* data = static_cast<ListBaton*>(req->data);
v8::Local<v8::Value> argv[2] = {
v8::Undefined(isolate),
v8::String::NewFromUtf8(isolate,"WORLD")
};
std::cout << "Sending callback" << std::endl;
data->callback->Call(2,argv);
}
void init (v8::Handle<v8::Object> target) {
NODE_SET_METHOD(target, "hello", Hello);
}
NODE_MODULE(HelloNan, init);
If you guys could help me with this, I would really appreciate ...
+3
source to share
1 answer