c++ - How can I create another thread in the v8 -
i want set timeout v8::script::run. unfortunately,i have little experience v8. understood need use startpreemtion + loker + terminateexception. consequently, v8::script::run should in separate thread. calculation , control of execution time should in main thread. how can create thread in v8?. please me understand how it. here example of code it, function of thread doesn't start.
v8::local<v8::value> v8executestring( v8::handle<v8::string> source, v8::handle<v8::string> filename ) { // compiling script // ... // end compiling script dword start_tick = ::gettickcount(); v8::locker::startpreemption( 1 ); { v8::unlocker unlocker; boost::thread* th = new boost::thread( [&] () { v8::locker locker; v8::handlescope handle_scope; // running script // v8::script::run() // end running script }); } // calculation , control of execution time v8::locker locker; v8::handlescope handle_scope; while ( true ) { // terminate thread after 10 seconds if( ( (::gettickcount() - start_tick) / 1000 ) > 10 ) // v8::v8::terminateexception( ) } v8::locker::stoppreemption(); }
according this v8 bug report, startpreemption()
not reliable currently. however, don't need implement script execution timeout. program demonstrates way:
#include "v8.h" #include "ppltasks.h" void main(void) { auto isolate = v8::isolate::new(); { v8::locker locker(isolate); v8::isolate::scope isolatescope(isolate); v8::handlescope handlescope(isolate); auto context = v8::context::new(); { v8::context::scope contextscope(context); auto script = v8::script::compile(v8::string::new("while(true){}")); // terminate script in 5 seconds concurrency::create_task([isolate] { concurrency::wait(5000); v8::v8::terminateexecution(isolate); }); // run script script->run(); } context.dispose(); } isolate->dispose(); }
the timer implementation here suboptimal , specific windows concurrency runtime, it's example. luck!
Comments
Post a Comment