1# Thread Pool 2## Overview 3 4 5### Introduction 6Give a thread-safe thread pool. The thread-safe is for threadpool itself not for the threads in pool. An task queue and a thread group are under control. Users add tasks to task queue. The thread group will execute the tasks in task queue. 7 8 9`#include <thread_pool.h>` 10 11Inherits from OHOS::NoCopyable 12 13 14## Related Interfaces 15### OHOS::ThreadPool 16#### Public Functions 17| Return Type | Name | 18| -------------- | -------------- | 19| | **ThreadPool**(const std::string& name = std::string())<br>Construct ThreadPool. Name the threads in pool. | 20| | **~ThreadPool**() override | 21| void | **AddTask**(const Task& f)<br>Add a Task to task queue. If Start() has never been called, the Task will be executed immediately. | 22| size_t | **GetCurTaskNum**()<br>Get the current amount of tasks in task queue. | 23| size_t | **GetMaxTaskNum**() const<br>Get the maximum amount of tasks in task queue. | 24| std::string | **GetName**() const<br>Get the name of ThreadPool. | 25| size_t | **GetThreadsNum**() const<br>Get the current amount of threads in pool. | 26| void | **SetMaxTaskNum**(size_t maxSize)<br>Set the maximum amount of tasks in task queue. | 27| uint32_t | **Start**(int threadsNum)<br>Start a given number(threadNum) of threads, which will execute the tasks in task queue. | 28| void | **Stop**()<br>Stop ThreadPool and waiting all threads in pool to stop. | 29## Examples 301. Examples can be seen in base/test/unittest/common/utils_thread_pool_test.cpp 312. Running unit test: 32 33- Start developer test framework:[Developer Test - Using Test Framework](https://gitee.com/openharmony/testfwk_developer_test#using-test-framework) 34 35- Run this command in the test framework to run the tests of `thread_pool.h` 36 37```bash 38run -t UT -tp utils -ts UtilsThreadPoolTest 39``` 40## FAQ 411. **name**, the parameter of constructor, will be set as a part the real name of threads in pool. The real name of threads in pool will be like: name + No. The thread name is a meaningful C language string, whose length is restricted to 16 characters, including the terminating null byte ('\0'). Please pay attention to the length of name here. For example, if the number of threads in pool is less than 10, the maximum length of name is 14.