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!


On Fri, Jan 31, 2014 at 1:49 PM, Kevin Ingwersen
<[email protected]>wrote:

> 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
> Am Fr. Jan. 31 2014 22:27:04 schrieb Sara Abdelhameed:
>
> 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 :)
>
>
> On Fri, Jan 31, 2014 at 11:15 PM, Jochen Eisinger <[email protected]>wrote:
>
>> 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
>>
>>
>> On Fri, Jan 31, 2014 at 10:09 PM, Sara Abdelhameed <
>> [email protected]> wrote:
>>
>>> 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
>>>
>>>
>>>
>>> On Fri, Jan 31, 2014 at 11:45 AM, Sara Abdelhameed <
>>> [email protected]> wrote:
>>>
>>>> Thank you, and I'll try that now, wish it work with me.
>>>>
>>>>
>>>> On Fri, Jan 31, 2014 at 9:40 PM, Dmitry Lomov <[email protected]>wrote:
>>>>
>>>>>
>>>>>
>>>>>
>>>>> On Fri, Jan 31, 2014 at 11:38 AM, Sara Abdelhameed <
>>>>> [email protected]> 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.
>>>>>
>>>>>
>>>>>>
>>>>>> On Friday, January 31, 2014 11:23:28 AM UTC+2, Jochen Eisinger wrote:
>>>>>>
>>>>>>> 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 <
>>>>>>> [email protected]> 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
> [email protected]
> http://groups.google.com/group/v8-users
> ---
> 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
> [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
v8-users mailing list
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to