作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的最终目标是解决这个problem ,但我陷入了一些非常基本的东西。
我的整个C++ Module基本如下:
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()
。而是在主线程上注册一个回调。
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);
关于c++ - 为什么 "Isolate::GerCurrent()"在异步线程中为Null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33940750/
我是一名优秀的程序员,十分优秀!