2

I have to process whole bunch of JS scripts using C++ wrapper around V8 and I need to do it in parallel.

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.

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 ?

  • Not sure I get what the mutex is for and how you expect to have any kind of way to run them at the same time if there is a requirement they wouldn't run at the same time. Please elaborate. – Sami Kuhmonen Sep 23 '16 at 10:19
  • Mutex just in case v8 isn't thread safe. The long story is short - I need to run scripts in v8 simultaneously. – Eduard Bondarenko Sep 23 '16 at 12:22

1 Answers1

0

In order to execute two threads of javascript simultaneously, they must be in different v8::Isolate's. 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.

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.

xaxxon
  • 19,189
  • 5
  • 50
  • 80
  • Many thanks for your answer. Does v8::Locker create bottleneck ? Or two different v8::Isolate run indeed separate ? – Eduard Bondarenko Sep 23 '16 at 12:21
  • it has a cost associated with it, but that cost is unavoidable, so it doesn't really matter. As for whether it is a "bottleneck", that is determined by the whole of your system. – xaxxon Sep 23 '16 at 18:25
  • I've found another good answer related to this question http://stackoverflow.com/a/19394400/6135922. And it looks like you can run js scripts simultaneously via different contexts, not isolates. What do you think ? – Eduard Bondarenko Sep 25 '16 at 10:03
  • I think you cannot run multiple contexts from the same isolate at the same time. If you try it, you will see that v8::Locker stops them from running at the same time, or if you don't use Locker, that your program will crash immediately with a v8 error. – xaxxon Sep 25 '16 at 20:43
  • Thx. I think I need to try cuz I've a real problem. As I said earlier, I need to run scripts simultaneously and I thought that I can just use new isolate per script, but here the problem. Before run script I need to compile one (for performance purpose of course). But compilation goes in separate isolate than running..that's a real problem, I cannot access my compiled function from different isolate. Even if I will create pair during compilation like and try to run scrips in parallel way using correct isolate - it will run one script at a time (one thread per isolate) – Eduard Bondarenko Sep 25 '16 at 21:43
  • I don't understand your last sentence - if you compile the script for each isolate (), you can run them in parallel. v8::Locker is a per-isolate lock. – xaxxon Sep 25 '16 at 22:55
  • Yep, we can run different compile_func functions in parallerl, cuz they are placed in different isolates. But what if I want to run several instances of the same compiled_func in parallel ? – Eduard Bondarenko Sep 26 '16 at 05:41
  • "But what if I want to run several instances of the same compiled_func in parallel ?" That doesn't make any sense. Javascript doesn't have any concept of parallelism. The behavior would be completely undefined. You wouldn't need another javascript interpreter, you'd need a different language entirely. – xaxxon Sep 26 '16 at 05:49
  • Why not ? Yep, JS does not have, but I just want to start the same script in parallel. Imagine I have function F and I want to run it in two thread separate with complete separate data (thread1 -> F(data1), thread2 -> F(data2)). And my problem is that I need to compile function somewhere once and share cross isolates. – Eduard Bondarenko Sep 26 '16 at 06:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124176/discussion-between-eduard-bondarenko-and-xaxxon). – Eduard Bondarenko Sep 26 '16 at 06:22
  • I don't know how to tell you this any other way. You cannot do that. – xaxxon Sep 26 '16 at 07:12
  • Look, I appreciate your support, really. Many thanks. But, why we cannot create isolate per thread ? in our example (thread1 -> F(data1), thread2 -> F(data2)) it will be two isolates, one for thread1 and second for thread2. I mean we create thread, inside this thread we create new isolate and run code. But the problem is that I need to compile the same function F twice..inside isolate:thread1 and isolate:thread2. Any way to avoid it ? – Eduard Bondarenko Sep 26 '16 at 07:51
  • functions are per context, anyhow, not per isolate. – xaxxon Sep 26 '16 at 08:20