十三、v8 platform里thread pool使用

本文探讨了V8源码中DefaultPlatform类的线程池对象worker_threads_task_runner_的使用方法。通过一个封装函数,展示了如何在主线程外执行多个任务。在实现过程中遇到一个错误,即isolate的Enter()没有对应的Exit()调用,导致在 Dispose() 时出错。错误源于isolate_scope对象的作用域管理不当,未使用大括号限制其作用域,从而在isolate->Dispose()时未正确执行Exit()。
摘要由CSDN通过智能技术生成

        在v8源码 default-platform.h 中,DefaultPlatform类中有一个私有对象 worker_threads_task_runner_ ,是一个线程池对象,这里研究一下如何使用这个对象执行多个任务。

首先,参照hello-world.cc示例中的代码,因为isolate的需要和脚本执行在同一线程中,所以从isolate创建部分开始直至脚本执行完毕全部封装为一个函数。

内容不复杂,直接上源码吧。

main.cpp

// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "include/libplatform/libplatform.h"
#include "include/v8.h"

#include "task.h"
#include "waitgroup.h"

void ExecJsScript( WaitGroup* wg 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值