changeset 4:469c49d5aa02

this is the 2nd code
author Sara
date Sat, 01 Feb 2014 08:46:57 -0800
parents b3ad79b3197c
children a6cc4a802db6
files V8/Two_threads.cpp
diffstat 1 files changed, 63 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/V8/Two_threads.cpp	Sat Feb 01 08:46:57 2014 -0800
     1.3 @@ -0,0 +1,63 @@
     1.4 +#include <v8.h>
     1.5 +#include <pthread.h>
     1.6 +using namespace v8;
     1.7 +Isolate* isolate;
     1.8 +void* threadFunction(void*){
     1.9 +//Isolate* isolate1 = Isolate::New();
    1.10 +//{
    1.11 +  if(isolate == NULL){
    1.12 +   printf("Error1");
    1.13 +   return 0;
    1.14 +}
    1.15 +Locker locker1(isolate);
    1.16 +//isolate->Enter();
    1.17 +Isolate::Scope isolate_scope1(isolate);
    1.18 +HandleScope handle_scope1(isolate);
    1.19 +Handle<Context> context1 = Context::New(isolate);
    1.20 +  Context::Scope context_scope1(context1);
    1.21 +  Handle<String> source1 = String::NewFromUtf8(isolate, "'Hi' + ', Sara'");
    1.22 +  Handle<Script> script1 = Script::Compile(source1);
    1.23 +  Handle<Value> result1 = script1->Run();
    1.24 +  String::Utf8Value utf81(result1);
    1.25 +  printf("%s\n", *utf81);
    1.26 +//isolate->Exit();
    1.27 +Unlocker unlocker1(isolate);
    1.28 +
    1.29 +  return 0;
    1.30 +
    1.31 +}
    1.32 +int main(int argc, char* argv[]) {
    1.33 +
    1.34 +  // Get the default Isolate created at startup.
    1.35 +   isolate =  Isolate::New();
    1.36 +{
    1.37 +  if(isolate == NULL){
    1.38 +   printf("Error");
    1.39 +   return 0;
    1.40 +}
    1.41 +Locker locker(isolate);
    1.42 +Isolate::Scope isolate_scope(isolate);
    1.43 +  // Create a stack-allocated handle scope.
    1.44 +  HandleScope handle_scope(isolate);
    1.45 +  // Create a new context.
    1.46 +  Handle<Context> context = Context::New(isolate);
    1.47 +  // Enter the context for compiling and running the hello world script.
    1.48 +  Context::Scope context_scope(context);
    1.49 +  // Create a string containing the JavaScript source code.
    1.50 + //isolate->Exit();
    1.51 + Unlocker unlocker(isolate);
    1.52 + pthread_t thread_id;
    1.53 + pthread_create(&thread_id, NULL, &threadFunction,NULL);
    1.54 +//isolate->Enter();
    1.55 +  Handle<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
    1.56 +  // Compile the source code.
    1.57 +  Handle<Script> script = Script::Compile(source);
    1.58 +  // Run the script to get the result.
    1.59 +  Handle<Value> result = script->Run();
    1.60 +  // Convert the result to an UTF8 string and print it.
    1.61 +  String::Utf8Value utf8(result);
    1.62 +  printf("%s\n", *utf8);
    1.63 +  return 0;
    1.64 +}
    1.65 +//isolate->Dispose();
    1.66 +}