我有一个非常简单的程序,如下所示,它只是创建一个隔离,然后休眠:
#include <libplatform/libplatform.h>
#include <v8-platform.h>
#include <v8.h>
#include <stdio.h>
#include <unistd.h>
using v8::Isolate;
int main() {
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
Isolate* isolate = v8::Isolate::New(create_params);
printf("Sleeping...\n");
usleep(1000 * 1000 * 100);
printf("Done\n");
return 0;
}
当我运行这个程序时,我可以使用ps -T -p <process_id>
检查进程创建的线程数。 ,我看到在我的 8 核机器上,v8 创建了 7 个额外的线程,全部命名为“V8 WorkerThread”,在我的 16 核机器上,我创建了这个“V8 WorkerThread”的 8 个实例。
我希望了解是什么决定了 v8 产生的额外线程的数量以及这些线程的目的是什么。提前致谢!
最佳答案
当嵌入器(就是您!)未指定时,工作线程的数量是根据 CPU 核心的数量选择的。在 current implementation ,公式为:number_of_worker_threads = (number_of_CPU_cores - 1) 最多 8
,尽管这可能会更改,恕不另行通知。您还可以指定自己的工作线程池大小作为 NewDefaultPlatform
的参数。
工作线程用于可以在后台运行的各种任务,主要用于垃圾收集和优化编译。
关于v8 - 想要了解 V8 WorkerThread 的用途和数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56118604/