1.. _Initializing_and_Terminating_the_Library:
2
3Initializing and Terminating the Library
4========================================
5
6|full_name| automatically initializes the task scheduler.
7The initialization process is involved when a thread uses task scheduling services the first time,
8for example any parallel algorithm, flow graph or task group.
9The termination happens when the last such thread exits.
10
11Explicit Library Finalization
12*****************************
13
14oneTBB supports an explicit library termination as a preview feature.
15The ``oneapi::tbb::finalize`` function called with an instance of class ``oneapi::tbb::task_scheduler_handle`` blocks the calling thread
16until all worker threads implicitly created by the library have completed.
17If waiting for thread completion is not safe, e.g. may result in a deadlock
18or called inside a task, a parallel algorithm, or a flow graph node, the method fails.
19
20If you know how many active ``oneapi::tbb::task_scheduler_handle`` instances exist in the program,
21it is recommended to call ``oneapi::tbb::release`` function on all but the last one, then call ``oneapi::tbb::finalize`` for the last instance.
22
23