changeset 12:83548221c10d

the first test
author Sara
date Sat, 15 Feb 2014 11:58:46 -0800
parents 2fcaa90d17fc
children 77d9e57697b1
files V8_tests/create_two_context_inside_the_two_threads/ReadMe V8_tests/create_two_context_inside_the_two_threads/Test_1.cpp V8_tests/create_two_context_inside_the_two_threads/test1.PNG
diffstat 3 files changed, 96 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/V8_tests/create_two_context_inside_the_two_threads/ReadMe	Sat Feb 15 11:58:46 2014 -0800
     1.3 @@ -0,0 +1,4 @@
     1.4 +This is the first test which is; create the C++ strings of the two javascript functions.-] create two "child" threads of the current thread.-] give both threads the same birth function, but pass one string to one, the other string to the other-] inside the birth function:-] create a context, inside the isolate-] set the context active-] compile the string that was passed in-] execute the resulting compiled script-] end the thread.
     1.5 +I expect that to break.. run it a whole bunch of times, if it doesn't..
     1.6 +
     1.7 +this test works successfully and sometimes gives core dumped, and I captured the output at the screen shoot attached here in this folder. 
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/V8_tests/create_two_context_inside_the_two_threads/Test_1.cpp	Sat Feb 15 11:58:46 2014 -0800
     2.3 @@ -0,0 +1,92 @@
     2.4 +#include <v8.h>
     2.5 +#include <pthread.h>
     2.6 +using namespace v8;
     2.7 +Isolate* isolate;
     2.8 +//structure that holds thread's paramaters
     2.9 + struct threadParamaters{
    2.10 +//the string that holds the script to compile it
    2.11 + Handle<String> srcScript;
    2.12 + };
    2.13 +
    2.14 +void* threadFunction(void* passingParameter){
    2.15 +   if(isolate == NULL){
    2.16 +     printf("Error1");
    2.17 +     return 0;
    2.18 +    }
    2.19 +   //locks the current isolate, to be used in this thread only
    2.20 +   Locker locker1(isolate);
    2.21 +   //isolate->Enter();
    2.22 +  //create a new isolate scope for this thread
    2.23 +  Isolate::Scope isolate_scope1(isolate);//////////////////try without it
    2.24 +  //create new stack-allocated handle-scope
    2.25 +  HandleScope handle_scope1(isolate);
    2.26 +  //create new context for the current thread
    2.27 +  Handle<Context> context1 = Context::New(isolate);
    2.28 +  //Enters the current context
    2.29 +  Context::Scope context_scope1(context1);
    2.30 +  //passes the passed paramater to the current thread
    2.31 +  struct threadParamaters* p = (struct threadParamaters*) passingParameter;
    2.32 +  //compile and hold the compiled thread
    2.33 +  Handle<Script> script1 = Script::Compile(p->srcScript);
    2.34 + //executes the compiled thread
    2.35 +  Handle<Value> result1 = script1->Run();
    2.36 + //return the last expression of the executed script
    2.37 +  String::Utf8Value utf81(result1);
    2.38 +  printf("%s\n", *utf81);
    2.39 +  //isolate->Exit();
    2.40 +  //unlock the global isolate to be used by another thread 
    2.41 +  Unlocker unlocker1(isolate);
    2.42 +  return 0;
    2.43 +
    2.44 +}
    2.45 +int main(int argc, char* argv[]) {
    2.46 +   isolate =  Isolate::New();
    2.47 +{
    2.48 +   if(isolate == NULL){
    2.49 +     printf("Error");
    2.50 +     return 0;
    2.51 +    }
    2.52 +  //locks the current isolate, to be used in this thread only
    2.53 +  Locker locker(isolate);
    2.54 +  //create new handle-scope
    2.55 +  Isolate::Scope isolate_scope(isolate);
    2.56 +  // Create a stack-allocated handle scope.
    2.57 +  HandleScope handle_scope(isolate);
    2.58 +  // Create a new context.
    2.59 +  Handle<Context> context = Context::New(isolate);
    2.60 +  // Enter the context for compiling and running the hello world script.
    2.61 +  Context::Scope context_scope(context);
    2.62 +  // Create a string containing the JavaScript source code.
    2.63 +  //isolate->Exit();
    2.64 +  struct threadParamaters paramthread1;
    2.65 +  struct threadParamaters paramthread2;
    2.66 + //holds the scripts and passes it the threads
    2.67 + Handle<String> source1 = String::NewFromUtf8(isolate, "'Good' + ' Morning'");
    2.68 + paramthread1.srcScript = source1;
    2.69 + Handle<String> source2 = String::NewFromUtf8(isolate, "'Hi' + ', DrSean'");
    2.70 + paramthread2.srcScript = source2;
    2.71 + //unlocks the global isolate, to be used by the next thread
    2.72 + Unlocker unlocker(isolate);
    2.73 + pthread_t thread1, thread2;
    2.74 + //create the two threads
    2.75 + pthread_create(&thread1, NULL, &threadFunction,&paramthread1);
    2.76 +
    2.77 + //Note:::>> when I use join it didn't execute the script of the main thread
    2.78 +
    2.79 + //pthread_join(thread1, NULL);
    2.80 + pthread_create(&thread2, NULL, &threadFunction,&paramthread2);
    2.81 + //pthread_join(thread2, NULL);
    2.82 +  //Locker locker1(isolate);------------------------------------->>>>
    2.83 +  //isolate->Enter();
    2.84 +  Handle<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
    2.85 +  // Compile the source code.
    2.86 +  Handle<Script> script = Script::Compile(source);
    2.87 +  // Run the script to get the result.
    2.88 +  Handle<Value> result = script->Run();
    2.89 +  // Convert the result to an UTF8 string and print it.
    2.90 +  String::Utf8Value utf8(result);
    2.91 +  printf("%s\n", *utf8);
    2.92 +  return 0;
    2.93 +  }
    2.94 +  //isolate->Dispose();
    2.95 + }
     3.1 Binary file V8_tests/create_two_context_inside_the_two_threads/test1.PNG has changed