You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.4 KiB
87 lines
2.4 KiB
2 years ago
|
#include <future>
|
||
|
#include <functional>
|
||
2 years ago
|
#include "tiny_pool.h"
|
||
2 years ago
|
|
||
|
class TinyPool {
|
||
|
pool_t *pool;
|
||
2 years ago
|
static void wrap_c_func(void *func) {
|
||
|
(*static_cast<std::function<void()>*>(func))();
|
||
|
free(func);
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
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); }
|
||
2 years ago
|
|
||
2 years ago
|
template <typename Func, typename ...Args>
|
||
|
auto submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))>;
|
||
2 years ago
|
};
|
||
|
|
||
2 years ago
|
template <typename Func, typename ...Args>
|
||
|
auto TinyPool::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);
|
||
|
tiny_pool_submit(pool, TinyPool::wrap_c_func, (void*)(
|
||
|
new std::function<void()> (
|
||
|
[func_ptr]() { (*func_ptr)(); }
|
||
|
)
|
||
|
));
|
||
|
return func_ptr->get_future();
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
/// ------------------------------------ start test ------------------------------------
|
||
2 years ago
|
|
||
2 years ago
|
#include <iostream>
|
||
|
#include <unistd.h>
|
||
2 years ago
|
|
||
2 years ago
|
int test_func(char c) {
|
||
|
int num = c - '0';
|
||
|
printf("char -> `%c`\n", c);
|
||
|
for (int i = 0; i < num; ++i) {
|
||
|
printf("task %d running...\n", num);
|
||
|
usleep(500 * 1000);
|
||
|
}
|
||
|
return num;
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
int main() {
|
||
2 years ago
|
auto pool = TinyPool(3);
|
||
2 years ago
|
|
||
2 years ago
|
auto f0 = pool.submit(test_func, '0');
|
||
|
auto f1 = pool.submit(test_func, '1');
|
||
|
auto f2 = pool.submit(test_func, '2');
|
||
|
auto f3 = pool.submit(test_func, '3');
|
||
2 years ago
|
|
||
2 years ago
|
pool.boot();
|
||
2 years ago
|
|
||
2 years ago
|
auto f4 = pool.submit(test_func, '4');
|
||
|
auto f5 = pool.submit(test_func, '5');
|
||
2 years ago
|
|
||
2 years ago
|
printf("get future: %d\n", f0.get());
|
||
|
printf("get future: %d\n", f4.get());
|
||
|
printf("get future: %d\n", f3.get());
|
||
2 years ago
|
|
||
2 years ago
|
auto f6 = pool.submit(test_func, '6');
|
||
|
auto f7 = pool.submit(test_func, '7');
|
||
|
auto f8 = pool.submit(test_func, '8');
|
||
|
auto f9 = pool.submit(test_func, '9');
|
||
2 years ago
|
|
||
2 years ago
|
printf("get future: %d\n", f2.get());
|
||
|
printf("get future: %d\n", f5.get());
|
||
|
printf("get future: %d\n", f8.get());
|
||
2 years ago
|
|
||
2 years ago
|
pool.join();
|
||
2 years ago
|
|
||
2 years ago
|
printf("get future: %d\n", f6.get());
|
||
|
printf("get future: %d\n", f1.get());
|
||
|
printf("get future: %d\n", f9.get());
|
||
|
printf("get future: %d\n", f7.get());
|
||
2 years ago
|
}
|