简体   繁体   English

如何使用C ++中的V8同时运行一堆JS代码?

[英]How can I run a bunch of JS codes simultaneously using V8 from C++?

I have to process whole bunch of JS scripts using C++ wrapper around V8 and I need to do it in parallel. 我必须使用围绕V8的C ++包装器处理大量的JS脚本,我需要并行执行。

For example, I have two scripts A and B. I've compiled these scripts using C++ V8 functions and want to run it in parallel or at least in async way, so that script B does not wait when script A will be finished. 例如,我有两个脚本A和B.我使用C ++ V8函数编译这些脚本,并希望并行或至少以异步方式运行它们,这样脚本B就不会等待脚本A完成。

Now I am thinking to use threads, but in this case I have to use mutex, and that means the code will run consequentially (not what I want at all). 现在我想使用线程,但在这种情况下我必须使用互斥,这意味着代码将运行(不是我想要的)。

Maybe there is some ability to run js code asynchronously from C++ using V8.. Anyway, is there is any way I could run several scripts and they will work in parallel ? 也许有一些能力使用V8从C ++异步运行js代码..无论如何,有什么方法可以运行多个脚本并且它们将并行工作?

In order to execute two threads of javascript simultaneously, they must be in different v8::Isolate's. 为了同时执行两个javascript线程,它们必须位于不同的v8 :: Isolate中。 This means they cannot share any data or have any shared dependencies. 这意味着他们无法共享任何数据或具有任何共享依赖项。

This also requires the use of v8::Locker, to lock an isolate from being used by another context while it runs. 这还需要使用v8 :: Locker来锁定隔离区在运行时被另一个上下文使用。

Whether you use additional mutexes is up to you program's data requirements, but is not required for running multiple threads of execution within v8 -- just an isolate per thread. 是否使用其他互斥锁取决于程序的数据要求,但在v8中运行多个执行线程不是必需的 - 每个线程只需一个隔离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM