gpt4 book ai didi

javascript - 用于多线程的 v8 储物柜

转载 作者:可可西里 更新时间:2023-11-01 18:38:53 30 4
gpt4 key购买 nike

我在主线程上有一个函数,用于注册来自另一个 native 线程的回调。目前我遇到了本地线程无法访问 v8::Isolate 的问题。我四处搜索,似乎我们需要使用 v8::Locker 进行多线程处理。我想知道我们应该如何使用它?我是 V8 的新手,找不到好的文档可以引用。

代码:

Main thread:
void register_signaling_xml_callback(const Nan::FunctionCallbackInfo<v8::Value> &info) {
wa_call_register_signaling_xml_callback(details::wa_send_signaling_xml_cb, isolate); // Register the callback on native thread;
....
// v8::Unlocker unlocker(isolate); If I remove this comment, native thread can access the isolate. However, the below line will crash.
Nan::New(...);
}


Native thread:
int wa_send_signaling_data_cb(void *data, int len, void *userdata) {
....
Nan::EscapableHandleScope scope; // This line will crash due to can not get v8::Isolate.
}

如果我在主线程中放置 v8::Locker v8Locker(isolate) 然后 v8::Unlocker unlocker(isolate);, native 线程可以访问 v8::Isolate。然而,主线程似乎失去了对 isolate 的控制,Nan::New 将导致主线程崩溃。

最佳答案

通常在使用资源的地方进行锁定。在这种情况下,锁定应该发生在回调函数中:

Native thread:
int wa_send_signaling_data_cb(void *data, int len, void *userdata) {
....
v8::Locker v8Locker(); // no argument means default isolate
Nan::EscapableHandleScope scope; // This line will crash due to can not get v8::Isolate.
}

关于javascript - 用于多线程的 v8 储物柜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55602695/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com