想要了解 V8 的 WorkerThread(s) 的目的和数量

Wanting to understand purpose and count of V8's WorkerThread(s)

我有一个非常简单的程序如下,它只是创建一个隔离,然后休眠:

#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) up to a maximum of 8,尽管这可能会更改,恕不另行通知。您还可以将自己的工作线程池大小指定为 NewDefaultPlatform.

的参数

工作线程用于可在后台 运行 执行的各种任务,主要用于垃圾收集和优化编译。