From 90e96d84a324cd4899ecac127256a2e23c0706d0 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 5 Feb 2023 15:35:07 +0800 Subject: [PATCH] feat: oop thread pool with the c++0x --- demo.cc | 135 +++++++++++++++++++++++++++++++++++++++++++----- src/tiny_pool.h | 7 ++- 2 files changed, 129 insertions(+), 13 deletions(-) diff --git a/demo.cc b/demo.cc index a053e30..d042a6d 100644 --- a/demo.cc +++ b/demo.cc @@ -1,7 +1,11 @@ #include -#include +#include #include "tiny_pool.h" +#include +#include +#include + void demo_func(void *arg) { std::cout << *(int*)arg << std::endl; @@ -10,24 +14,131 @@ void demo_func(void *arg) { } +int test_func(char c) { + + + printf("char -> `%c`\n", c); + + return 233; + +} + + +void convert(void *f) { + +// auto *func = ( std::function * ) f; +// +// (*func)(); + + ( *static_cast< std::function* >(f) )(); + +// auto t = (std::function*)task; +// +// (*t)(NULL); + + free(f); + +} + +class TinyPool { + pool_t *pool; +public: + + TinyPool(uint32_t size); + + ~TinyPool(); + + + template + auto submit(Func &&func, Args &&...args) -> std::future { + + std::function wrap_func = std::bind( + std::forward(func), std::forward(args)... + ); + + auto func_ptr = std::make_shared< + std::packaged_task + >(wrap_func); + +// std::function task_func = [func_ptr](void*) { +// (*func_ptr)(); +// }; + + auto *t = new std::function; + *t = [func_ptr](){ + (*func_ptr)(); + }; + + + // TODO: run task_func + +// (*t)(); + +// convert( (void*)t ); + +// auto t = reinterpret_cast(task_func); + + tiny_pool_submit(pool, convert, (void*)t); + + return func_ptr->get_future(); + + } + + + void boot() { + tiny_pool_boot(pool); + } + +}; + + + + + +TinyPool::TinyPool(uint32_t size) { + + pool = tiny_pool_create(size); + +// if (pool == (void*)0) { // NULL in c-style +// throw std::runtime_error("thread pool create error"); +// } + +} + +TinyPool::~TinyPool() { + tiny_pool_kill(pool); +} + int main() { std::cout << "tiny thread pool demo start" << std::endl; + auto p = TinyPool(1); + + auto f = p.submit(test_func, 'A'); + + p.boot(); + + std::cout << "get future: " << f.get() << std::endl; + - auto pool = tiny_pool_create(3); +// auto f = submit(test_func, 'D'); - int dat[] = {0, 1, 2, 3, 4, 5, 6}; - tiny_pool_submit(pool, demo_func, (void*)&dat[0]); - tiny_pool_submit(pool, demo_func, (void*)&dat[1]); - tiny_pool_submit(pool, demo_func, (void*)&dat[2]); - tiny_pool_submit(pool, demo_func, (void*)&dat[3]); - tiny_pool_submit(pool, demo_func, (void*)&dat[4]); - tiny_pool_submit(pool, demo_func, (void*)&dat[5]); - tiny_pool_submit(pool, demo_func, (void*)&dat[6]); +// auto pool = tiny_pool_create(3); - tiny_pool_boot(pool); +// int dat[] = {0, 1, 2, 3, 4, 5, 6}; +// tiny_pool_submit(pool, demo_func, (void*)&dat[0]); +// tiny_pool_submit(pool, demo_func, (void*)&dat[1]); +// tiny_pool_submit(pool, demo_func, (void*)&dat[2]); +// tiny_pool_submit(pool, demo_func, (void*)&dat[3]); +// tiny_pool_submit(pool, demo_func, (void*)&dat[4]); +// tiny_pool_submit(pool, demo_func, (void*)&dat[5]); +// tiny_pool_submit(pool, demo_func, (void*)&dat[6]); +// +// tiny_pool_boot(pool); +// +// tiny_pool_join(pool); - tiny_pool_join(pool); + sleep(10); std::cout << "tiny thread pool demo exit" << std::endl; return 0; diff --git a/src/tiny_pool.h b/src/tiny_pool.h index 11b148c..6738362 100644 --- a/src/tiny_pool.h +++ b/src/tiny_pool.h @@ -1,9 +1,14 @@ #ifndef TINY_POOL_H_ #define TINY_POOL_H_ -#include #include +#ifndef __cplusplus +#include #include +#else +#include +#include +#endif /// This is a lightweight thread pool designed for linux. It is implemented in C language. Its /// goal is to provide simple and stable services, so it does not provide functions such as priority