mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 years ago
7 changed files with 40 additions and 79 deletions
@ -1,21 +0,0 @@ |
|||
#include <thread> |
|||
#include "global_utils.h" |
|||
|
|||
const static uint32_t DEFAULT_THREAD_NUM = 1; |
|||
|
|||
range_split_t range_split(uint64_t start, uint64_t end, uint64_t size) { |
|||
uint64_t rear = end - (end - start) % size; // (rear - start) % size == 0
|
|||
range_split_t ranges; |
|||
for (uint64_t i = start; i < rear; i += size) { |
|||
ranges.emplace_back(i, i + size); |
|||
} |
|||
if (rear != end) { |
|||
ranges.emplace_back(rear, end); |
|||
} |
|||
return ranges; |
|||
} |
|||
|
|||
uint32_t thread_num() { |
|||
auto num = std::thread::hardware_concurrency(); // CPU core number
|
|||
return (num == 0) ? DEFAULT_THREAD_NUM : num; // fetch failed -> use default number
|
|||
} |
@ -1,44 +1,18 @@ |
|||
#pragma once |
|||
|
|||
#include <future> |
|||
#include <vector> |
|||
#include <functional> |
|||
#include "tiny_pool.h" |
|||
|
|||
uint32_t thread_num(); |
|||
|
|||
typedef std::vector<std::pair<uint64_t, uint64_t>> range_split_t; |
|||
range_split_t range_split(uint64_t start, uint64_t end, uint64_t size); |
|||
|
|||
class TinyPool { // OOP for tiny_thread_pool
|
|||
pool_t *pool; |
|||
static void wrap_c_func(void *func) { // wrap std::function as c-style function ptr
|
|||
auto func_ptr = static_cast<std::function<void()>*>(func); |
|||
(*func_ptr)(); |
|||
delete func_ptr; // free lambda function
|
|||
inline range_split_t range_split(uint64_t start, uint64_t end, uint64_t size) { |
|||
uint64_t rear = end - (end - start) % size; // (rear - start) % size == 0
|
|||
range_split_t ranges; |
|||
for (uint64_t i = start; i < rear; i += size) { |
|||
ranges.emplace_back(i, i + size); |
|||
} |
|||
|
|||
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); } |
|||
// TODO: thread pool destroy
|
|||
|
|||
template <typename Func, typename ...Args> |
|||
auto submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))> { // submit new task
|
|||
std::function<decltype(func(args...))()> wrap_func = std::bind( |
|||
std::forward<Func>(func), std::forward<Args>(args)... // wrap as a function without params
|
|||
); |
|||
auto func_ptr = std::make_shared< |
|||
std::packaged_task<decltype(func(args...))()> // function task as shared ptr
|
|||
>(wrap_func); |
|||
tiny_pool_submit(pool, TinyPool::wrap_c_func, (void*)( // submit with thread pool interface
|
|||
new std::function<void()> ( |
|||
[func_ptr]() { (*func_ptr)(); } // create lambda for running task
|
|||
) |
|||
)); |
|||
return func_ptr->get_future(); // return future object
|
|||
if (rear != end) { |
|||
ranges.emplace_back(rear, end); |
|||
} |
|||
}; |
|||
return ranges; |
|||
} |
|||
|
@ -1 +1 @@ |
|||
Subproject commit 755faf4116ecd046b1a9278e292ea91ba8ef8e7c |
|||
Subproject commit 40068b99d6406ec0d0418773f0080c2ee247f165 |
Loading…
Reference in new issue