我想为 v8::Script::Run 设置超时。不幸的是,我对 v8 有一点经验。我知道我需要使用 StartPreemtion + Loker + TerminateException。因此,v8::Script::Run 应该在一个单独的线程中。执行时间的计算和控制应该在主线程中。如何在 v8 中创建另一个线程?请帮助我了解如何做到这一点。这是我这样做的代码示例,但是线程的功能没有启动。
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 and control of the 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();
}