Browse Source

feat: oop thread pool with the c++0x

master
Dnomd343 1 year ago
parent
commit
90e96d84a3
  1. 135
      demo.cc
  2. 7
      src/tiny_pool.h

135
demo.cc

@ -1,7 +1,11 @@
#include <iostream>
#include <unistd.h>
#include <stdexcept>
#include "tiny_pool.h"
#include <unistd.h>
#include <future>
#include <functional>
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<void()> * ) f;
//
// (*func)();
( *static_cast< std::function<void()>* >(f) )();
// auto t = (std::function<void(void*)>*)task;
//
// (*t)(NULL);
free(f);
}
class TinyPool {
pool_t *pool;
public:
TinyPool(uint32_t size);
~TinyPool();
template <typename Func, typename ...Args>
auto submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))> {
std::function<decltype(func(args...))()> wrap_func = std::bind(
std::forward<Func>(func), std::forward<Args>(args)...
);
auto func_ptr = std::make_shared<
std::packaged_task<decltype(func(args...))()>
>(wrap_func);
// std::function<void(void*)> task_func = [func_ptr](void*) {
// (*func_ptr)();
// };
auto *t = new std::function<void()>;
*t = [func_ptr](){
(*func_ptr)();
};
// TODO: run task_func
// (*t)();
// convert( (void*)t );
// auto t = reinterpret_cast<void(void*)>(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;

7
src/tiny_pool.h

@ -1,9 +1,14 @@
#ifndef TINY_POOL_H_
#define TINY_POOL_H_
#include <stdint.h>
#include <pthread.h>
#ifndef __cplusplus
#include <stdint.h>
#include <stdbool.h>
#else
#include <cstdint>
#include <cstdbool>
#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

Loading…
Cancel
Save