1

私の最終的な目標はこの問題を解決することですが、かなり基本的なことで行き詰まっています。

私の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);

AsyncWorkisolateを呼び出した後に NULL になるのはなぜですか?Isolate::GetCurrent()

4

1 に答える 1

1

わかりました、間違った方法で設定したようIsolate::GetCurrent()で、ワーカースレッドを呼び出すべきではありません。代わりに、メイン スレッドにコールバックを登録します。

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);
于 2015-11-26T14:47:17.523 に答える