Posting a
code-snip that show you how to multi-thread ”hello World” in V8. It
isn’t that complex once you understand that V8 contain it’s own
preemtive scheduler and is part operating system in nature.
The big upside is
the next step – because threads are a budget on any PC, and we need the
threads for the voice transport in FS – so I will use a pool with 10
processes x 10 threads and throw a RTOS scheduler in front of each thread –
meaning each thread will in reality proceess 100-1000 calls. This will solve
what have been my main heacache so far - the CCXML/VXML threading budget. It
should also work smoothly with FS as it don’t start competing with FS on
the thread budget.
The only drawback
is that this will not run on a Sparc, but well . . .
// Lock V8 so we single thread access to
V8. You MUST
// use the V8 Locker scope for this
v8::Locker
locker;
// Start the preemtive scheduler with a
10ms timer. This only
// needs calling once, but adding it here
don't hurt.
locker.StartPreemption(10);
// Set scope.
HandleScope
handle_scope;
// Create this threads contect inside the
scope.
Persistent<Context>
context = Context::New();
Context::Scope
context_scope(context);
// create a source-code variable to hold
javascript.
Handle<String>
source = String::New("for(j
= 0; j<100;j++)\n”
”i='Hello'
+ ', World!'");
// So far we have just created the basics
we need.
// Now we will start a rt-loop where we
compile & run.
Handle<Script>
script;
Handle<Value>
result;
for(x=1;;x++)
{
// we need to handle the new scope so V8
knows when
// it can re-use memory
HandleScope
handle_scope;
// and we compile
script
= Script::Compile(source);
// and we execute
result
= script->Run();
Jan