Google Javascript v8 - 多线程

发布于 2024-08-31 13:37:25 字数 787 浏览 3 评论 0原文

假设我有以下一段代码,

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::Lockerv8::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨洒年华 2024-09-07 13:37:25

根据 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.

小女人ら 2024-09-07 13:37:25

请查看这篇文章。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文