Thread 고치기

This commit is contained in:
USW
2025-04-30 12:01:37 +09:00
parent 89633f94a2
commit cebd6bb9e9
4 changed files with 42 additions and 24 deletions

View File

@@ -16,9 +16,9 @@ int main() {
auto config = Chattr::ConfigManager::load(); auto config = Chattr::ConfigManager::load();
Chattr::log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount); Chattr::log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount);
struct Chattr::TCPSocket sock; Chattr::TCPSocket sock;
struct Chattr::Address serveraddr; struct Chattr::Address serveraddr;
struct Chattr::TCPSocket clientSock; Chattr::TCPSocket clientSock;
struct Chattr::Address clientAddr; struct Chattr::Address clientAddr;
if (config.ipVersion == 4) { if (config.ipVersion == 4) {
sock.init(AF_INET); sock.init(AF_INET);
@@ -59,7 +59,7 @@ int main() {
spdlog::info("Waiting for connection..."); spdlog::info("Waiting for connection...");
sock.accept(clientSock, clientAddr); sock.accept(clientSock, clientAddr);
threadPool.enqueueJob(_TCPClient, std::move(clientSock), clientAddr); // threadPool.enqueueJob(_TCPClient, std::move(clientSock), clientAddr);
Chattr::Thread thread_(_TCPClient, std::move(clientSock), clientAddr); Chattr::Thread thread_(_TCPClient, std::move(clientSock), clientAddr);
thread_.detach(); thread_.detach();

View File

@@ -16,15 +16,8 @@ Thread::~Thread() {
spdlog::critical("There is not joined thread"); spdlog::critical("There is not joined thread");
std::exit(EXIT_FAILURE); std::exit(EXIT_FAILURE);
} }
} if (returnValuePtr != nullptr)
delete returnValuePtr;
void* Thread::join() {
#ifdef _WIN32
WaitForSingleObject(handle_, INFINITE);
#elif __linux__
pthread_join(handle_, returnValue);
#endif
return returnValue;
} }
void Thread::detach() { void Thread::detach() {

View File

@@ -7,7 +7,7 @@ ThreadPool::ThreadPool(std::uint32_t numThreads) {
workers_.reserve(numThreads); workers_.reserve(numThreads);
while (numThreads--) while (numThreads--)
workers_.push_back([this]() -> void* { return this->Worker(); }); workers_.push_back([this]() -> int { this->Worker(); return 1; });
} }
ThreadPool::~ThreadPool() { ThreadPool::~ThreadPool() {
@@ -24,7 +24,6 @@ void* ThreadPool::Worker() {
#elif __linux__ #elif __linux__
pid_t pid = getpid(); pid_t pid = getpid();
#endif #endif
spdlog::info("ThreadPool Worker : {}", pid);
while (!terminate_) { while (!terminate_) {
std::unique_lock<std::mutex> lock(jobQueueMutex); std::unique_lock<std::mutex> lock(jobQueueMutex);
spdlog::info("ThreadPool Worker : {} Waiting for a job", pid); spdlog::info("ThreadPool Worker : {} Waiting for a job", pid);

View File

@@ -23,7 +23,7 @@ public:
} }
#elif __linux__ #elif __linux__
static void* thread_func(void *param) { static void* thread_func(void *param) {
auto task = static_cast<std::function<void* ()>*>(param); auto task = static_cast<std::function<void()>*>(param);
(*task)(); (*task)();
delete task; delete task;
return 0; return 0;
@@ -36,15 +36,11 @@ public:
Thread& operator=(Thread&) = delete; Thread& operator=(Thread&) = delete;
template<typename _Callable, typename... _Args> template<typename _Callable, typename... _Args>
requires (!std::is_same_v<std::decay_t<_Callable>, Thread>) requires (!std::is_same_v<std::decay_t<_Callable>, Thread>) &&
(!std::is_void_v<std::invoke_result_t<_Callable, _Args...>>)
Thread(_Callable&& __f, _Args&&... __args) { Thread(_Callable&& __f, _Args&&... __args) {
auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable { auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable {
if constexpr (!std::is_void_v<std::invoke_result_t<decltype(__f), decltype(__args)...>>) { returnValuePtr = new std::invoke_result_t<_Callable, _Args...>(__f(std::move(__args)...));
returnValue = __f(std::move(__args)...);
}
else {
__f(std::move(__args)...); // void 반환은 저장하지 않음
}
}; };
std::function<void()> funcPtr = std::move(boundFunc); std::function<void()> funcPtr = std::move(boundFunc);
#ifdef _WIN32 #ifdef _WIN32
@@ -53,9 +49,39 @@ public:
pthread_create(&handle_, NULL, thread_func, funcPtr); pthread_create(&handle_, NULL, thread_func, funcPtr);
#endif #endif
} }
/*template<typename _Callable, typename... _Args>
requires (!std::is_same_v<std::decay_t<_Callable>, Thread>) &&
std::is_void_v<std::invoke_result_t<_Callable, _Args...>>
Thread(_Callable&& __f, _Args&&... __args) {
auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable {
__f(std::move(__args)...);
};
std::function<void()> funcPtr = std::move(boundFunc);
#ifdef _WIN32
handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, new std::function<void()>(std::move(funcPtr)), 0, nullptr);
#elif __linux__
pthread_create(&handle_, NULL, thread_func, funcPtr);
#endif
}*/
~Thread(); ~Thread();
void* join(); void join() {
#ifdef _WIN32
WaitForSingleObject(handle_, INFINITE);
#elif __linux__
pthread_join(handle_, returnValue);
#endif
}
template<typename _RetType>
requires (!std::is_void_v<_RetType>)
_RetType join() {
#ifdef _WIN32
WaitForSingleObject(handle_, INFINITE);
#elif __linux__
pthread_join(handle_, returnValue);
#endif
return *static_cast<_RetType *>(returnValuePtr);
}
void detach(); void detach();
private: private:
@@ -64,7 +90,7 @@ private:
#elif __linux__ #elif __linux__
pthread_t handle_; pthread_t handle_;
#endif #endif
void* returnValue = nullptr; void* returnValuePtr = nullptr;
bool detached = false; bool detached = false;
}; };