# Thread pool work scheduling

libuv provides a threadpool which can be used to run user code and get notified in the loop thread. This thread pool is internally used to run all file system operations, as well as getaddrinfo and getnameinfo requests.

Its default size is 4, but it can be changed at startup time by setting the `UV_THREADPOOL_SIZE` environment variable to any value (the absolute maximum is 1024).

Changed in version 1.30.0: the maximum UV_THREADPOOL_SIZE allowed was increased from 128 to 1024.

Changed in version 1.45.0: threads now have an 8 MB stack instead of the (sometimes too low) platform default.

Changed in version 1.50.0: threads now have a default name of libuv-worker.

The threadpool is global and shared across all event loops. When a particular function makes use of the threadpool (e.g. when using `uv_queue_work()`) libuv preallocates and initializes the maximum number of threads allowed by `UV_THREADPOOL_SIZE`. More threads usually means more throughput but a higher memory footprint. Thread stacks grow lazily on most platforms though.

Note

Note that even though a global thread pool which is shared across all events loops is used, the functions are not thread safe.

## Data types

type uv_work_t

  Work request type.

typedef void (*uv_work_cb)(uv_work_t *req)

  Callback passed to `uv_queue_work()` which will be run on the thread pool.

typedef void (*uv_after_work_cb)(uv_work_t *req, int status)

  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 using `uv_cancel()` status will be `UV_ECANCELED`.

### Public members

uv_loop_t *uv_work_t.loop

  Loop that started this request and where completion will be reported. Readonly.

See also

The `uv_req_t` members also apply.

## API

int uv_queue_work(uv_loop_t *loop, uv_work_t *req, uv_work_cb work_cb, uv_after_work_cb after_work_cb)

  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()`.

See also

The `uv_req_t` API functions also apply.