Threadpool requires having a name

This commit is contained in:
Nikita Lisitsa 2020-09-21 20:22:22 +03:00
parent fb4922c6e9
commit 1ab980f0d2
3 changed files with 12 additions and 5 deletions

View file

@ -5,4 +5,4 @@ file(GLOB_RECURSE PSEMEK_UTIL_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "so
add_library(psemek-util ${PSEMEK_UTIL_HEADERS} ${PSEMEK_UTIL_SOURCES}) add_library(psemek-util ${PSEMEK_UTIL_HEADERS} ${PSEMEK_UTIL_SOURCES})
target_include_directories(psemek-util PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") target_include_directories(psemek-util PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(psemek-util PUBLIC ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(psemek-util PUBLIC psemek-log ${CMAKE_THREAD_LIBS_INIT})

View file

@ -6,17 +6,19 @@
#include <future> #include <future>
#include <vector> #include <vector>
#include <string>
namespace psemek::util namespace psemek::util
{ {
struct threadpool struct threadpool
{ {
threadpool() threadpool(std::string const & name)
: threadpool(std::max(1u, std::thread::hardware_concurrency())) : threadpool(name, std::max(1u, std::thread::hardware_concurrency()))
{} {}
threadpool(std::size_t thread_count) threadpool(std::string const & name, std::size_t thread_count)
: name(name)
{ {
start(thread_count); start(thread_count);
} }
@ -50,6 +52,7 @@ namespace psemek::util
} }
private: private:
std::string const name;
std::vector<util::thread> threads; std::vector<util::thread> threads;
util::synchronized_queue<movable_function<void()>> tasks_queue; util::synchronized_queue<movable_function<void()>> tasks_queue;
}; };

View file

@ -1,5 +1,8 @@
#include <psemek/util/threadpool.hpp> #include <psemek/util/threadpool.hpp>
#include <psemek/util/unused.hpp> #include <psemek/util/unused.hpp>
#include <psemek/util/to_string.hpp>
#include <psemek/log/log.hpp>
namespace psemek::util namespace psemek::util
{ {
@ -8,8 +11,9 @@ namespace psemek::util
{ {
for (std::size_t th = 0; th < thread_count; ++th) for (std::size_t th = 0; th < thread_count; ++th)
{ {
threads.emplace_back([this] threads.emplace_back([this, th]
{ {
log::register_thread(to_string(name, '#', th));
while (true) while (true)
{ {
auto task = tasks_queue.pop(); auto task = tasks_queue.pop();