原文:http://docs.libuv.org/en/latest/threadpool.html
libuv提供的threadpool可以用来在循环线程中运行用户代码和得到通知。这个内部线程池是用来运行所有的文件系统操作,以及getaddrinfo getnameinfo请求。默认大小是4,但它在启动时可以改变UV_THREADPOOL_SIZE环境变量来设置为任何值(最大是128)。threadpool是全局的,为所有事件循环共享。当一个特定的函数利用threadpool(即使用uv_queue_work())时,libuv预分配和初始化UV_THREADPOOL_SIZE所允许的最大线程数。这将导致一个相对较小的内存开销:128个线程~ 1 mb,但增加了线程在运行时的性能。
注意:所有事件循环共享一个全局的线程池,但这些函数(libuv的线程池API)并不是线程安全的。
Data types
-
Work request type.
uv_work_t
-
void
-
Callback passed to
uv_queue_work()
which will be run on the thread pool.
(*uv_work_cb)
(
uv_work_t*
req
)
-
void
-
Callback passed to
uv_queue_work()
which will be called on the loop thread after the work on the threadpool has been completed. If the work was cancelled usinguv_cancel()
status will beUV_ECANCELED
.
(*uv_after_work_cb)
(
uv_work_t*
req, int
status
)
API
-
int
-
Initializes a work request which will run the given work_cb in a thread from the threadpool. Once work_cb is completed,after_work_cb will be called on the loop thread.
This request can be cancelled with
uv_cancel()
.
uv_queue_work
(
uv_loop_t*
loop,
uv_work_t*
req,
uv_work_cb
work_cb,
uv_after_work_cb
after_work_cb
)