view V8/Two_threads.cpp @ 4:469c49d5aa02

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