Google Javascript v8 - 多线程
假设我有以下一段代码,
bool run (void)
{
HandleScope hande_scope;
Handle<String> source;
Local<Script> script;
Persistent<Context> context;
context = Context::New();
Context::Scope context_scope(context);
script = Script::Compile("var a = 1; var b = 2;");
Local<Value> result = script->Run();
if (result.IsEmpty())
return false;
else
return true;
}
是否真的无法使用多线程执行这段代码?似乎 HandleScope
并非设计用于多线程应用程序。 我可以使用 v8::Locker
和 v8::Unlocker
方法,但这总是会给我执行跟踪,如下所示:
t1: a = 1
t1: b = 2
t2: a = 1
t2: b = 2
我希望有人能给我一个关于获取这个的提示多线程代码,以便可能的执行跟踪如下所示:
t1: a = 1
t2: a = 1
t1: b = 2
t2: b = 2
Suppose I have the following piece of code
bool run (void)
{
HandleScope hande_scope;
Handle<String> source;
Local<Script> script;
Persistent<Context> context;
context = Context::New();
Context::Scope context_scope(context);
script = Script::Compile("var a = 1; var b = 2;");
Local<Value> result = script->Run();
if (result.IsEmpty())
return false;
else
return true;
}
Is it true that one cannot execute this code using multiple threads? It seems like HandleScope
is not designed to be used in multithreaded applications.
I can use the v8::Locker
and v8::Unlocker
methodes but that would always give me execution traces as this:
t1: a = 1
t1: b = 2
t2: a = 1
t2: b = 2
I hope someone can give me a hint on getting this code multithreaded so that a possible execution trace could like this:
t1: a = 1
t2: a = 1
t1: b = 2
t2: b = 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 v8 问题 510,一项名为“隔离”的功能几个月前添加到后备箱。这应该允许在单个进程中存在 v8 的多个(非交互)实例。
According to v8 issue 510, a feature called "Isolates" was added to the trunk some months back. This should allow multiple (non-interacting) instances of v8 in a single process.
请查看这篇文章。 v8 引擎有一个 Locker 类,可让您抢占代码。这样您就可以使用多个线程。
Look at this post. The v8 engine has a Locker class, that lets you preempt your code. With this you can use multiple threads.