为什么“Isolate :: GerCurrent()”在asynchronous线程中为空?

我的最终目标是解决这个问题 ,但我陷入了一些非常基本的东西。

我的整个C ++模块基本如下:

void AsyncWork(void *arg) { Isolate* isolate = Isolate::GetCurrent(); // isolate is NULL if (isolate != NULL) { HandleScope scope(isolate); } else { printf("isolate is null\n"); return; } // ... } void testAsync(const FunctionCallbackInfo<Value>& args) { uv_thread_t id; int data = 10; uv_thread_create(&id, AsyncWork, &data); } void init(Handle<Object> target) { NODE_SET_METHOD(target, "testAsync", testAsync); } NODE_MODULE(MyCppModule, init); 

在AsyncWork中调用Isolate::GetCurrent()之后,为什么isolate NULL?

好吧,似乎我设置了错误的方式,不应该在工作线程中调用Isolate::GetCurrent() 。 而是在主线程上注册一个callback。

 static uv_async_t async; static int i; void AsyncWork(void *arg) { for (i = 0; i < 5; ++i) { async.data = (void*)&i; uv_async_send(&async); Sleep(1000); } } void testCallback(uv_async_t *handle) { Isolate* isolate = Isolate::GetCurrent(); if (isolate != NULL) { HandleScope scope(isolate); printf("Yay\n"); } else { printf("isolate is null\n"); } int data = *((int*)handle->data); printf("data: %d\n", data); } void testAsync(const FunctionCallbackInfo<Value>& args) { uv_thread_t id; int data = 10; uv_async_init(uv_default_loop(), &async, testCallback); uv_thread_create(&id, AsyncWork, &data); } void init(Handle<Object> target) { NODE_SET_METHOD(target, "testAsync", testAsync); } NODE_MODULE(MyCppModule, init);