尝试在多线程中创建隔离时V8引发错误

时间:2019-07-05 15:45:54

标签: c++ c multithreading v8

我正在尝试在多线程中创建新的Isolate并出现错误。 说明:

# Fatal error in , line 0
# Check failed: embedded_blob() == StickyEmbeddedBlob().

代码:

#include <iostream>
#include <thread>
#include "include/libplatform/libplatform.h"
#include "include/v8.h"

using namespace v8;
using namespace std;

typedef void* v8_Isolate;

unique_ptr<Platform> v8_latform;


void __cdecl V8_Initialize()
{
    v8_latform = platform::NewDefaultPlatform();

    V8::InitializePlatform(v8_latform.get());
    V8::Initialize();
}

void __cdecl V8_Destroy()
{
    V8::Dispose();
    V8::ShutdownPlatform();
}

v8_Isolate __cdecl V8_New_Isolate()
{
    Isolate::CreateParams create_params = Isolate::CreateParams();

    create_params.array_buffer_allocator = ArrayBuffer::Allocator::NewDefaultAllocator();

    return (v8_Isolate)Isolate::New(create_params);
}

void __cdecl V8_Dispose_Isolate(v8_Isolate isolate)
{
    ((Isolate*)isolate)->Dispose();
}

class thread_class {
public:
    void operator()()
    {
        auto isolate = V8_New_Isolate();        
        cout << "isolate created\n";
        V8_Dispose_Isolate(isolate);
        cout << "isolate destroyed\n";
    }
};

void create_threads()
{
    for (int i = 0; i < 5; ++i) {
        thread_class cls;
        thread thd(cls);

        thd.detach();
    }
}

int main()
{
    V8_Initialize();

    create_threads();
    this_thread::sleep_for(5s);

    V8_Destroy();

    return 0;
}

0 个答案:

没有答案