feat add thread_pool
This commit is contained in:
15
src/system/thread_pool.cc
Normal file
15
src/system/thread_pool.cc
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "sled/system/thread_pool.h"
|
||||
|
||||
namespace sled {
|
||||
ThreadPool::ThreadPool(int num_threads)
|
||||
{
|
||||
if (num_threads == -1) {
|
||||
num_threads = std::thread::hardware_concurrency();
|
||||
}
|
||||
scheduler = new sled::Scheduler(
|
||||
sled::Scheduler::Config().setWorkerThreadCount(num_threads));
|
||||
}
|
||||
|
||||
ThreadPool::~ThreadPool() { delete scheduler; }
|
||||
|
||||
}// namespace sled
|
20
src/system/thread_pool_bench.cc
Normal file
20
src/system/thread_pool_bench.cc
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "sled/system/fiber/wait_group.h"
|
||||
#include <benchmark/benchmark.h>
|
||||
#include <future>
|
||||
#include <sled/system/thread_pool.h>
|
||||
|
||||
static void
|
||||
ThreadPoolBench(benchmark::State &state)
|
||||
{
|
||||
sled::ThreadPool pool(-1);
|
||||
for (auto _ : state) {
|
||||
std::vector<std::future<int>> futures;
|
||||
for (int i = 0; i < state.range(0); i++) {
|
||||
std::future<int> f = pool.submit([]() { return 1; });
|
||||
futures.push_back(std::move(f));
|
||||
}
|
||||
for (auto &f : futures) { f.get(); }
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK(ThreadPoolBench)->RangeMultiplier(10)->Range(10, 10000);
|
65
src/system/thread_pool_test.cc
Normal file
65
src/system/thread_pool_test.cc
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <random>
|
||||
#include <sled/system/thread_pool.h>
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 mt(rd());
|
||||
std::uniform_int_distribution<int> dist(-10, 10);
|
||||
auto rnd = std::bind(dist, mt);
|
||||
|
||||
void
|
||||
simulate_hard_computation()
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(20 + rnd()));
|
||||
}
|
||||
|
||||
// Simple function that adds multiplies two numbers and prints the result
|
||||
void
|
||||
multiply(const int a, const int b)
|
||||
{
|
||||
simulate_hard_computation();
|
||||
const int res = a * b;
|
||||
}
|
||||
|
||||
// Same as before but now we have an output parameter
|
||||
void
|
||||
multiply_output(int &out, const int a, const int b)
|
||||
{
|
||||
simulate_hard_computation();
|
||||
out = a * b;
|
||||
}
|
||||
|
||||
// Same as before but now we have an output parameter
|
||||
int
|
||||
multiply_return(const int a, const int b)
|
||||
{
|
||||
simulate_hard_computation();
|
||||
const int res = a * b;
|
||||
return res;
|
||||
}
|
||||
|
||||
class ThreadPoolTest : public ::testing::Test {
|
||||
public:
|
||||
void SetUp() override { tp = new sled::ThreadPool(); }
|
||||
|
||||
void TearDown() override { delete tp; }
|
||||
|
||||
sled::ThreadPool *tp;
|
||||
};
|
||||
|
||||
TEST_F(ThreadPoolTest, Output)
|
||||
{
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
int out;
|
||||
tp->submit(multiply_output, std::ref(out), i, i).get();
|
||||
EXPECT_EQ(out, i * i);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(ThreadPoolTest, Return)
|
||||
{
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
auto f = tp->submit(multiply_return, i, i);
|
||||
EXPECT_EQ(f.get(), i * i);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user