#include <v8.h>
#include <pthread.h>
using namespace v8;
Isolate* isolate;
void* threadFunction(void*)
 {
   //Isolate* isolate1 = Isolate::New();
   //{
   if(isolate == NULL)
    {
      printf("Error1");
      return 0;
    }

   Locker locker1(isolate);
   //isolate->Enter();
   Isolate::Scope isolate_scope1(isolate);
   HandleScope handle_scope1(isolate);
   Handle<Context> context1 = Context::New(isolate);
   Context::Scope context_scope1(context1);
   Handle<String> source1 = String::NewFromUtf8(isolate, "'Hi' + ', Sara'");
   Handle<Script> script1 = Script::Compile(source1);
   Handle<Value> result1 = script1->Run();
   String::Utf8Value utf81(result1);
   printf("%s\n", *utf81);
   //isolate->Exit();
   Unlocker unlocker1(isolate);

   return 0;
 }
 
int main(int argc, char* argv[]) 
 {
   // Get the default Isolate created at startup.
   isolate =  Isolate::New();
   if(isolate == NULL)
    {
      printf("Error");
      return 0;
    }
   //Sara: the "locker" looks like it's purpose is to make sure only a single
   // thread is modifying the isolate..  this goes against the purpose, which
   // is for multiple scripts to be running at the same time..
   Locker locker(isolate);
   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.
   //isolate->Exit();
   Unlocker unlocker(isolate);
   pthread_t thread_id;
   pthread_create(&thread_id, NULL, &threadFunction,NULL);
   //isolate->Enter();
   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;
   //isolate->Dispose();
 }
