原创作者: mryufeng   阅读:2929次   评论:0条   更新时间:2011-06-01    
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 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics