• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2.. _threadpool:
3
4Thread pool work scheduling
5===========================
6
7libuv provides a threadpool which can be used to run user code and get notified
8in the loop thread. This thread pool is internally used to run all file system
9operations, as well as getaddrinfo and getnameinfo requests.
10
11Its default size is 4, but it can be changed at startup time by setting the
12``UV_THREADPOOL_SIZE`` environment variable to any value (the absolute maximum
13is 1024).
14
15.. versionchanged:: 1.30.0 the maximum UV_THREADPOOL_SIZE allowed was increased from 128 to 1024.
16
17The threadpool is global and shared across all event loops. When a particular
18function makes use of the threadpool (i.e. when using :c:func:`uv_queue_work`)
19libuv preallocates and initializes the maximum number of threads allowed by
20``UV_THREADPOOL_SIZE``. This causes a relatively minor memory overhead
21(~1MB for 128 threads) but increases the performance of threading at runtime.
22
23.. note::
24    Note that even though a global thread pool which is shared across all events
25    loops is used, the functions are not thread safe.
26
27
28Data types
29----------
30
31.. c:type:: uv_work_t
32
33    Work request type.
34
35.. c:type:: void (*uv_work_cb)(uv_work_t* req)
36
37    Callback passed to :c:func:`uv_queue_work` which will be run on the thread
38    pool.
39
40.. c:type:: void (*uv_after_work_cb)(uv_work_t* req, int status)
41
42    Callback passed to :c:func:`uv_queue_work` which will be called on the loop
43    thread after the work on the threadpool has been completed. If the work
44    was cancelled using :c:func:`uv_cancel` `status` will be ``UV_ECANCELED``.
45
46
47Public members
48^^^^^^^^^^^^^^
49
50.. c:member:: uv_loop_t* uv_work_t.loop
51
52    Loop that started this request and where completion will be reported.
53    Readonly.
54
55.. seealso:: The :c:type:`uv_req_t` members also apply.
56
57
58API
59---
60
61.. c:function:: int uv_queue_work(uv_loop_t* loop, uv_work_t* req, uv_work_cb work_cb, uv_after_work_cb after_work_cb)
62
63    Initializes a work request which will run the given `work_cb` in a thread
64    from the threadpool. Once `work_cb` is completed, `after_work_cb` will be
65    called on the loop thread.
66
67    This request can be cancelled with :c:func:`uv_cancel`.
68
69.. seealso:: The :c:type:`uv_req_t` API functions also apply.
70