Use optional instead of unique_ptr in ThreadPool

This commit is contained in:
Stephen Seo 2022-06-16 14:09:26 +09:00
parent 3e6fd6c1a5
commit 6a4be98216

View file

@ -6,8 +6,8 @@
#include <deque> #include <deque>
#include <functional> #include <functional>
#include <list> #include <list>
#include <memory>
#include <mutex> #include <mutex>
#include <optional>
#include <queue> #include <queue>
#include <thread> #include <thread>
#include <tuple> #include <tuple>
@ -23,8 +23,8 @@ namespace Internal {
using TPFnType = std::function<void(void *)>; using TPFnType = std::function<void(void *)>;
using TPTupleType = std::tuple<TPFnType, void *>; using TPTupleType = std::tuple<TPFnType, void *>;
using TPQueueType = std::queue<TPTupleType>; using TPQueueType = std::queue<TPTupleType>;
using ThreadPtr = std::unique_ptr<std::thread>; using ThreadOpt = std::optional<std::thread>;
using ThreadStackType = std::vector<std::tuple<ThreadPtr, std::thread::id>>; using ThreadStackType = std::vector<std::tuple<ThreadOpt, std::thread::id>>;
using ThreadStacksType = std::deque<ThreadStackType>; using ThreadStacksType = std::deque<ThreadStackType>;
using ThreadStacksMutexesT = std::deque<std::mutex>; using ThreadStacksMutexesT = std::deque<std::mutex>;
using ThreadCountersT = std::deque<std::atomic_uint>; using ThreadCountersT = std::deque<std::atomic_uint>;
@ -82,7 +82,7 @@ class ThreadPool {
std::mutex *threadStackMutex = std::get<1>(pointers); std::mutex *threadStackMutex = std::get<1>(pointers);
std::atomic_uint *aCounter = std::get<2>(pointers); std::atomic_uint *aCounter = std::get<2>(pointers);
for (unsigned int i = 0; i < MAXSIZE; ++i) { for (unsigned int i = 0; i < MAXSIZE; ++i) {
std::thread *newThread = new std::thread( std::thread newThread(
[](Internal::ThreadStackType *threadStack, [](Internal::ThreadStackType *threadStack,
std::mutex *threadStackMutex, std::mutex *threadStackMutex,
Internal::TPQueueType *fnQueue, std::mutex *queueMutex, Internal::TPQueueType *fnQueue, std::mutex *queueMutex,
@ -91,7 +91,7 @@ class ThreadPool {
{ {
std::lock_guard<std::mutex> lock(*threadStackMutex); std::lock_guard<std::mutex> lock(*threadStackMutex);
threadStack->push_back( threadStack->push_back(
{Internal::ThreadPtr(nullptr), {Internal::ThreadOpt{},
std::this_thread::get_id()}); std::this_thread::get_id()});
} }
@ -149,7 +149,7 @@ class ThreadPool {
std::this_thread::sleep_for(std::chrono::microseconds(15)); std::this_thread::sleep_for(std::chrono::microseconds(15));
} }
std::lock_guard<std::mutex> stackLock(*threadStackMutex); std::lock_guard<std::mutex> stackLock(*threadStackMutex);
std::get<0>(threadStack->at(i)).reset(newThread); std::get<0>(threadStack->at(i)) = std::move(newThread);
} }
return pointers; return pointers;
} else { } else {