Async线程pool及其作用

erlang能够利用多核心cpu的基础设施有2个 1. 进程调度器 2. async 线程池。

其中 async 线程池主要设计用来 能够在driver里面异步的执行费时操作, 因为driver是在调度器里面调用的 不过费时操作的话 会block掉整个调度器 而且调度器资源有限。

细节参见 erl_async.c

Driver API:

Asynchronous calls
The latest Erlang versions (R7B and later) has provision for asynchronous function calls, using a thread pool provided by Erlang. There is also a select call, that can be used for asynchronous drivers.

long driver_async (ErlDrvPort port, unsigned int* key, void (*async_invoke)(void*), void* async_data, void (*async_free)(void*))

void ready_async(ErlDrvData drv_data, ErlDrvThreadData thread_data)


其中erlang自己的file driver就大量依赖于异步线程池 所以如果文件操作密集型的程序可以考虑加大池的数量。

erl有2个参数和这个池有关:

+a size
Suggested stack size, in kilowords, for threads in the async-thread pool. Valid range is 16-8192 kilowords. The default suggested stack size is 16 kilowords, i.e, 64 kilobyte on 32-bit architectures. This small default size has been chosen since the amount of async-threads might be quite large. The default size is enough for drivers delivered with Erlang/OTP, but might not be sufficiently large for other dynamically linked in drivers that use the driver_async() functionality. Note that the value passed is only a suggestion, and it might even be ignored on some platforms.
(大量池的时候 要考虑栈内存的影响 缩小这个值)

+A size
Sets the number of threads in async thread pool, valid range is 0-1024. Default is 0.

总结起来就是如果需要大量的费时操作 又需要原生的Api来做的话 可以考虑写个driver来利用这个特性。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值