annotate V8_tests/Two_threads.cpp @ 14:7eb1a37c12da

the two parallel threads with two different isolates
author Sara
date Sat, 15 Feb 2014 13:17:49 -0800
parents
children
rev   line source
seanhalle@6 1 #include <v8.h>
seanhalle@6 2 #include <pthread.h>
seanhalle@6 3 using namespace v8;
seanhalle@6 4 Isolate* isolate;
seanhalle@6 5 void* threadFunction(void*)
seanhalle@6 6 {
seanhalle@6 7 //Isolate* isolate1 = Isolate::New();
seanhalle@6 8 //{
seanhalle@6 9 if(isolate == NULL)
seanhalle@6 10 {
seanhalle@6 11 printf("Error1");
seanhalle@6 12 return 0;
seanhalle@6 13 }
seanhalle@6 14
seanhalle@6 15 Locker locker1(isolate);
seanhalle@6 16 //isolate->Enter();
seanhalle@6 17 Isolate::Scope isolate_scope1(isolate);
seanhalle@6 18 HandleScope handle_scope1(isolate);
seanhalle@6 19 Handle<Context> context1 = Context::New(isolate);
seanhalle@6 20 Context::Scope context_scope1(context1);
seanhalle@6 21 Handle<String> source1 = String::NewFromUtf8(isolate, "'Hi' + ', Sara'");
seanhalle@6 22 Handle<Script> script1 = Script::Compile(source1);
seanhalle@6 23 Handle<Value> result1 = script1->Run();
seanhalle@6 24 String::Utf8Value utf81(result1);
seanhalle@6 25 printf("%s\n", *utf81);
seanhalle@6 26 //isolate->Exit();
seanhalle@6 27 Unlocker unlocker1(isolate);
seanhalle@6 28
seanhalle@6 29 return 0;
seanhalle@6 30 }
seanhalle@6 31
seanhalle@6 32 int main(int argc, char* argv[])
seanhalle@6 33 {
seanhalle@6 34 // Get the default Isolate created at startup.
seanhalle@6 35 isolate = Isolate::New();
seanhalle@6 36 if(isolate == NULL)
seanhalle@6 37 {
seanhalle@6 38 printf("Error");
seanhalle@6 39 return 0;
seanhalle@6 40 }
seanhalle@6 41 //Sara: the "locker" looks like it's purpose is to make sure only a single
seanhalle@6 42 // thread is modifying the isolate.. this goes against the purpose, which
seanhalle@6 43 // is for multiple scripts to be running at the same time..
seanhalle@6 44 Locker locker(isolate);
seanhalle@6 45 Isolate::Scope isolate_scope(isolate);
seanhalle@6 46 // Create a stack-allocated handle scope.
seanhalle@6 47 HandleScope handle_scope(isolate);
seanhalle@6 48 // Create a new context.
seanhalle@6 49 Handle<Context> context = Context::New(isolate);
seanhalle@6 50 // Enter the context for compiling and running the hello world script.
seanhalle@6 51 Context::Scope context_scope(context);
seanhalle@6 52 // Create a string containing the JavaScript source code.
seanhalle@6 53 //isolate->Exit();
seanhalle@6 54 Unlocker unlocker(isolate);
seanhalle@6 55 pthread_t thread_id;
seanhalle@6 56 pthread_create(&thread_id, NULL, &threadFunction,NULL);
seanhalle@6 57 //isolate->Enter();
seanhalle@6 58 Handle<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
seanhalle@6 59 // Compile the source code.
seanhalle@6 60 Handle<Script> script = Script::Compile(source);
seanhalle@6 61 // Run the script to get the result.
seanhalle@6 62 Handle<Value> result = script->Run();
seanhalle@6 63 // Convert the result to an UTF8 string and print it.
seanhalle@6 64 String::Utf8Value utf8(result);
seanhalle@6 65 printf("%s\n", *utf8);
seanhalle@6 66 return 0;
seanhalle@6 67 //isolate->Dispose();
seanhalle@6 68 }