Thread pool

FossilOrigin-Name: 24dc2ba043050c5a3bc454d2496d5f934a19dacee5a18feb946c68f3e3adf872
This commit is contained in:
nekobit 2022-10-21 15:01:42 +00:00
parent 7e91a4a4ce
commit ab163ce4dd
4 changed files with 105 additions and 0 deletions

View file

@ -25,6 +25,7 @@ add_library(wormhole_lib
src/config/config_db.cpp
src/config/config_loader.cpp
src/config/config_instance.cpp
src/type/thread_pool.cpp
src/user.cpp
src/control.cpp
src/logger.cpp)

View file

@ -26,6 +26,9 @@
namespace HTTP
{
/**
* @brief A request sender wrapping Curl Multi
*/
class RequestSender
{
public:

43
src/type/thread_pool.cpp Normal file
View file

@ -0,0 +1,43 @@
/*
* Wormhole - Federated social network
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "thread_pool.h"
using namespace Threads;
ThreadPool::ThreadPool(size_t size)
: pool{size},
it_smallest_pool{pool.begin()}
{}
// Sets `it_smallest_pool' to the smallest pool available (based on ThreadInfo::children)
void ThreadPool::set_smallest_pool()
{
auto cur_pool = pool.begin();
size_t count = cur_pool->children;
for (auto i = pool.begin(); i != pool.end(); ++i)
if (i->children < count)
{
cur_pool = i;
count = i->children;
}
it_smallest_pool = cur_pool;
}

58
src/type/thread_pool.h Normal file
View file

@ -0,0 +1,58 @@
/*
* Wormhole - Federated social network
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <thread>
#include <vector>
namespace Threads
{
class ThreadPool
{
struct ThreadInfo
{
int children; /// Represents how many "members" in a pool
std::thread thr;
};
public:
using threadpool_t = std::vector<ThreadInfo>;
ThreadPool(size_t size);
~ThreadPool() = default;
template <class Function, class... Args>
void start(Function&& f, Args&&... args)
{
for (size_t i = 0; i < pool.size(); ++i)
{
pool[i].thr = std::thread{f, &args..., i};
}
}
/// Gets a pool with the least amount of children ("smallest" thread)
inline ThreadPool::ThreadInfo& select_pool() { return *it_smallest_pool; }
inline size_t get_size() const noexcept { return pool.size(); }
private:
/// Only calculate the smallest pool when new pools are added
void set_smallest_pool();
threadpool_t pool;
threadpool_t::iterator it_smallest_pool;
};
}