@ -18,7 +18,7 @@ These four life cycles must proceed sequentially: `PREPARING` `->` `RUNNING` `->
> NOTE: The STOPPING and EXITING states are automatically managed by the thread pool, and users do not need to care about them.
### Usage
## Usage
When using it, you need to use `tiny_pool_create` to create a thread pool first, and then use the `tiny_pool_submit` function to submit tasks to it. After the preparation is completed, use `tiny_pool_boot` to start the operation of the thread pool, and then you can also add other tasks.
@ -26,9 +26,146 @@ When preparing to exit, you have two options. The first is to use `tiny_pool_joi
In addition, there is a `tiny_pool_kill`, this command will directly clear all tasks, kill all threads, all ongoing work will be canceled, it should only be used in emergency situations (such as a fatal error in the main program). In other cases, it is recommended to use `tiny_pool_join` or `tiny_pool_detach` interface.
### Demo
## Demo
### Compile
This is a basic c-based use, file at `demo/c/demo.c`:
```c
#include<stdio.h>
#include<unistd.h>
#include "tiny_pool.h"
void test_fun(void *i) {
int num = *(int*)i;
printf("task %d start\n", num);
for (int t = 0; t <num;++t){
sleep(1);
printf("task %d running...\n", num);
}
printf("task %d complete\n", num);
}
int main() {
int dat[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
pool_t *pool = tiny_pool_create(4);
tiny_pool_submit(pool, test_fun, (void*)&dat[0]);
tiny_pool_submit(pool, test_fun, (void*)&dat[1]);
tiny_pool_boot(pool);
sleep(5);
tiny_pool_submit(pool, test_fun, (void*)&dat[2]);
tiny_pool_submit(pool, test_fun, (void*)&dat[3]);
tiny_pool_submit(pool, test_fun, (void*)&dat[4]);
tiny_pool_submit(pool, test_fun, (void*)&dat[5]);
tiny_pool_submit(pool, test_fun, (void*)&dat[6]);
sleep(6);
tiny_pool_submit(pool, test_fun, (void*)&dat[7]);
tiny_pool_submit(pool, test_fun, (void*)&dat[8]);
tiny_pool_join(pool);
return 0;
}
```
In C++, the C ABI interface can be called directly. Here, OOP is used to repackage, which provides stronger functions under C++11, file at `demo/cpp/demo.cc`:
```cpp
#include<future>
#include<functional>
#include "tiny_pool.h"
class TinyPool {
pool_t *pool;
static void wrap_c_func(void *func) {
(*static_cast<std::function<void()>*>(func))();
free(func);
}
public:
void boot() { tiny_pool_boot(pool); }
void join() { tiny_pool_join(pool); }
void kill() { tiny_pool_kill(pool); }
void detach() { tiny_pool_detach(pool); }
explicit TinyPool(uint32_t size) { pool = tiny_pool_create(size); }
template <typenameFunc,typename...Args>
auto submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))>;
};
template <typenameFunc,typename...Args>
auto TinyPool::submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))> {