Does V8 engine support multithreading programming?

7,154 views
Skip to first unread message

Sara Abdelhameed

unread,
Jan 31, 2014, 3:51:38 AM1/31/14
to v8-u...@googlegroups.com
Hello all, 
does v8 engine support multithreaded application ? and could I run two different javascript code in two different threads at the same time ? 

Kevin Ingwersen

unread,
Jan 31, 2014, 4:17:28 AM1/31/14
to v8-u...@googlegroups.com
Nodejs seems to do threading, but uses just one context after all. But it seems to still multithread after all O.o You should give it a try, because I heared that v8 is threadsafe.
Am Fr. Jan. 31 2014 09:51:38 schrieb Sara Abdelhameed:

> Hello all,
> does v8 engine support multithreaded application ? and could I run two different javascript code in two different threads at the same time ?
>
> --



Jochen Eisinger

unread,
Jan 31, 2014, 4:23:28 AM1/31/14
to v8-u...@googlegroups.com
There are two options: (1) use different isolates on each thread (then the scripts can run in parallel) and (2) use one isolate and use v8::Locker to lock the isolate before you use it (then only one thread at a time can execute scripts)

best
-jochen


On Fri, Jan 31, 2014 at 9:51 AM, Sara Abdelhameed <saraabdel...@gmail.com> wrote:
Hello all, 
does v8 engine support multithreaded application ? and could I run two different javascript code in two different threads at the same time ? 

--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Sara Abdelhameed

unread,
Jan 31, 2014, 2:38:46 PM1/31/14
to v8-u...@googlegroups.com
so, if I want to make very simple example such as having two threads that each one run different script and don't depend on each other, and I want them to be in parallel, so I must use two isolate, for every thread there is one isolate and no need to use lock as the scripts are different and independent from each other. is this right? 

Dmitry Lomov

unread,
Jan 31, 2014, 2:40:50 PM1/31/14
to v8-u...@googlegroups.com
On Fri, Jan 31, 2014 at 11:38 AM, Sara Abdelhameed <saraabdel...@gmail.com> wrote:
so, if I want to make very simple example such as having two threads that each one run different script and don't depend on each other, and I want them to be in parallel, so I must use two isolate, for every thread there is one isolate and no need to use lock as the scripts are different and independent from each other. is this right? 
 
Yes that is correct.

Sara Abdelhameed

unread,
Jan 31, 2014, 2:45:24 PM1/31/14
to v8-u...@googlegroups.com
Thank you, and I'll try that now, wish it work with me. 


You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/oN_3tVBd3H4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.

Sara Abdelhameed

unread,
Jan 31, 2014, 4:09:37 PM1/31/14
to v8-u...@googlegroups.com
I have tried these but it wasn't working with me,
The code is:
#include <v8.h>
#include <pthread.h>
using namespace v8;
void* threadFunction(void*){
Isolate *isolate1 = Isolate::GetCurrent();
HandleScope handle_scope1(isolate1);
Handle<Context> context1 = Context::New(isolate1);
  Context::Scope context_scope1(context1);
  Handle<String> source1 = String::NewFromUtf8(isolate1, "'Hi' + ', Sara'");
  Handle<Script> script1 = Script::Compile(source1);
  Handle<Value> result1 = script1->Run();
  String::Utf8Value utf81(result1);
  printf("%s\n", *utf81);
  return 0;
}
int main(int argc, char* argv[]) {
  Isolate* isolate = Isolate::GetCurrent();
  HandleScope handle_scope(isolate);
  Handle<Context> context = Context::New(isolate);
  Context::Scope context_scope(context);
 pthread_t thread_id;
pthread_create(&thread_id, NULL, &threadFunction,NULL);
  Handle<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
  Handle<Script> script = Script::Compile(source);
  Handle<Value> result = script->Run();
  String::Utf8Value utf8(result);
  printf("%s\n", *utf8);
  return 0;
}


and the command that I used to compile is
g++ -Iinclude Two_threads.cpp -o Two_threads -Wl,--start-group out/native/obj.target/{tools/gyp/libv8_{base.ia32,snapshot},third_party/icu/libicu{uc,i18n,data}}.a -Wl,--end-group -lrt
and to execute is
 ./Two_threads

it was compiled without giving me any error but when I executed it i printed only hello world! "the javascript string of the main thread" and didn't print hi,Sara the javascript string of the single thread I have in the code.

So, what is the problem? or what ?

Thank you in advance

Jochen Eisinger

unread,
Jan 31, 2014, 4:15:49 PM1/31/14
to v8-u...@googlegroups.com
Instead of using Isolate::GetCurrent() you have to create the isolates explicitly (at least on all but the main threads) and enter them, e.g. using an isolate scope.

best
-jochen

Sara Abdelhameed

unread,
Jan 31, 2014, 4:27:04 PM1/31/14
to v8-u...@googlegroups.com
Sorry, I don't get what you exactly meant, did you mean I create for every thread including the main thread and enter them using an isolate scope? or what I didn't understand  "(at least on all but the main threads) "?

Thank you in advance :) 

Kevin Ingwersen

unread,
Jan 31, 2014, 4:49:50 PM1/31/14
to v8-u...@googlegroups.com
He was just saying that you need to create one isolate for each new thread, so that you can make a context in each thread. With that, the threads can run independent from the other :) So you need to make it so, that thread1 has an own isolate, thread2 has an own isolate, and so on and so forth. :3

Sara Abdelhameed

unread,
Feb 1, 2014, 4:58:08 AM2/1/14
to v8-u...@googlegroups.com
I tried to work with Isolate::New() for each thread "including the main thread" and then Isolate::Scope but it behaved as before, only printed hello, world of the main thread and didn't print hi,Sara of the 2nd thread.
is this because they need a synchronization as they can't work in the same time so I must use v8::Lock? or what is the problem?

here is the code:
#include <v8.h>
#include <pthread.h>
using namespace v8;
void* threadFunction(void*){
Isolate* isolate1 = Isolate::New();
  if(isolate1 == NULL){
   printf("Error1");
   return 0;
}
Isolate::Scope isolate_scope1(isolate1);

HandleScope handle_scope1(isolate1);
Handle<Context> context1 = Context::New(isolate1);
  Context::Scope context_scope1(context1);
  Handle<String> source1 = String::NewFromUtf8(isolate1, "'Hi' + ', Sara'");
  Handle<Script> script1 = Script::Compile(source1);
  Handle<Value> result1 = script1->Run();
  String::Utf8Value utf81(result1);
  printf("%s\n", *utf81);
  return 0;
}
int main(int argc, char* argv[]) {

  // Get the default Isolate created at startup.
  Isolate* isolate =  Isolate::New();;
  if(isolate == NULL){
   printf("Error");
   return 0;
}
Isolate::Scope isolate_scope(isolate);
  // Create a stack-allocated handle scope.
  HandleScope handle_scope(isolate);
  // Create a new context.

  Handle<Context> context = Context::New(isolate);
  // Enter the context for compiling and running the hello world script.
  Context::Scope context_scope(context);
  // Create a string containing the JavaScript source code.

 pthread_t thread_id;
 pthread_create(&thread_id, NULL, &threadFunction,NULL);
  Handle<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
  // Compile the source code.

  Handle<Script> script = Script::Compile(source);
  // Run the script to get the result.

  Handle<Value> result = script->Run();
  // Convert the result to an UTF8 string and print it.

  String::Utf8Value utf8(result);
  printf("%s\n", *utf8);
  return 0;
}



and the command i used to compile,
g++ -Iinclude Two_threads.cpp -o Two_threads -Wl,--start-group out/native/obj.target/{tools/gyp/libv8_{base.ia32,snapshot},third_party/icu/libicu{uc,i18n,data}}.a -Wl,--end-group -lrt
and to execute it:
./Two_threads

the output is:
Hello, World!


Peter Olsson

unread,
Feb 8, 2014, 3:36:58 AM2/8/14
to v8-u...@googlegroups.com
Maybe the application exits before the thread has been executed? Try to join the thread before executing the application.

/Peter

Jorge Chamorro

unread,
Feb 12, 2014, 4:36:42 AM2/12/14
to v8-u...@googlegroups.com
TAGG: Threads à gogo for Node.js does exactly that: <http://github.com/xk/node-threads-a-gogo>

Kevin Ingwersen

unread,
Feb 13, 2014, 7:28:36 PM2/13/14
to v8-u...@googlegroups.com
Threads a gogo still use node-waf - so it cant be utilized by more modern nodejs…too bad :/

Jorge Chamorro

unread,
Feb 14, 2014, 8:31:09 AM2/14/14
to v8-u...@googlegroups.com
But pull requests are welcomed :-)

On 14/02/2014, at 01:28, Kevin Ingwersen wrote:

> Threads a gogo still use node-waf - so it cant be utilized by more modern nodejs...too bad :/

Kevin Ingwersen

unread,
Feb 14, 2014, 8:32:29 AM2/14/14
to v8-u...@googlegroups.com
Might be doing that, actually. :)
I need to kick off some native module’s start in a separate thread. so, i should utilize that. ^^

Jorge Chamorro

unread,
Feb 14, 2014, 9:14:08 AM2/14/14
to v8-u...@googlegroups.com
Cool :-)

But note that the node-waf/gyp issue isn't the only one: v8's APIs have changed a bit here and there, too... if I'm not mistaken.

Also there's the "pthreads not in windows" issue, and my experience in Windows programming is == NULL... :/

Please, guys, excuse me for the -slightly- off-topic.
--
( Jorge )();

Kevin Ingwersen

unread,
Feb 14, 2014, 9:17:10 AM2/14/14
to v8-u...@googlegroups.com
pthreads-w32 is the solution ^^. I use it with the php pthreads extension.
For what version did you originaly code this? Nodejs version I mean.

Jason Hill

unread,
Feb 26, 2014, 9:55:22 AM2/26/14
to v8-u...@googlegroups.com, ingwi...@googlemail.com
jxcore (a multithreaded nodejs distribution) uses multiple threads/v8 isolates/v8 contexts under the same process.

Jesper Jansson

unread,
Jul 3, 2014, 8:56:18 AM7/3/14
to v8-u...@googlegroups.com
Is it possible to have 2 different threads working on the same isolate if they never access each others values. I'm currently working on  a game engine where I want to be able to stream new map while the player can still play on the old map where it change map when everything is loaded. During the period the map is loaded it will only create new Objects that the user can only reach when the scene is fully loaded and by then the scene will be joined again. Also no JavaScript will be required by the thread streaming the scene other then creating new objects from c++. The only problem I can see is that they may access the same FunctionTemplate that I know will never be destroyed.

Jakob Kummerow

unread,
Jul 3, 2014, 9:56:43 AM7/3/14
to v8-u...@googlegroups.com
On Thu, Jul 3, 2014 at 2:56 PM, Jesper Jansson <jepp...@gmail.com> wrote:
Is it possible to have 2 different threads working on the same isolate

No.
 
For more options, visit https://groups.google.com/d/optout.

Torben Buelter

unread,
Dec 12, 2016, 3:52:06 AM12/12/16
to v8-users
Am Donnerstag, 3. Juli 2014 15:56:43 UTC+2 schrieb Jakob Kummerow:
On Thu, Jul 3, 2014 at 2:56 PM, Jesper Jansson <jepp...@gmail.com> wrote:
Is it possible to have 2 different threads working on the same isolate

No.

That's not true. I'm using the same isolate in ~10 Threads, though access is restricted through a mutex

Ben Noordhuis

unread,
Dec 12, 2016, 4:34:35 AM12/12/16
to v8-users
His reply is to someone asking if two threads can manipulate an
isolate in parallel without proper locking. 'No' is the right answer.

Sandeep kumar M

unread,
Jan 8, 2018, 6:17:31 AM1/8/18
to v8-users
I want to understand the reason behind acuire and release locks in j2v8 library
Reply all
Reply to author
Forward
0 new messages