Thread 고치기
This commit is contained in:
@@ -16,9 +16,9 @@ int main() {
|
||||
auto config = Chattr::ConfigManager::load();
|
||||
Chattr::log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount);
|
||||
|
||||
struct Chattr::TCPSocket sock;
|
||||
Chattr::TCPSocket sock;
|
||||
struct Chattr::Address serveraddr;
|
||||
struct Chattr::TCPSocket clientSock;
|
||||
Chattr::TCPSocket clientSock;
|
||||
struct Chattr::Address clientAddr;
|
||||
if (config.ipVersion == 4) {
|
||||
sock.init(AF_INET);
|
||||
@@ -59,7 +59,7 @@ int main() {
|
||||
spdlog::info("Waiting for connection...");
|
||||
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);
|
||||
thread_.detach();
|
||||
|
||||
@@ -16,15 +16,8 @@ Thread::~Thread() {
|
||||
spdlog::critical("There is not joined thread");
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void* Thread::join() {
|
||||
#ifdef _WIN32
|
||||
WaitForSingleObject(handle_, INFINITE);
|
||||
#elif __linux__
|
||||
pthread_join(handle_, returnValue);
|
||||
#endif
|
||||
return returnValue;
|
||||
if (returnValuePtr != nullptr)
|
||||
delete returnValuePtr;
|
||||
}
|
||||
|
||||
void Thread::detach() {
|
||||
|
||||
@@ -7,7 +7,7 @@ ThreadPool::ThreadPool(std::uint32_t numThreads) {
|
||||
workers_.reserve(numThreads);
|
||||
|
||||
while (numThreads--)
|
||||
workers_.push_back([this]() -> void* { return this->Worker(); });
|
||||
workers_.push_back([this]() -> int { this->Worker(); return 1; });
|
||||
}
|
||||
|
||||
ThreadPool::~ThreadPool() {
|
||||
@@ -24,7 +24,6 @@ void* ThreadPool::Worker() {
|
||||
#elif __linux__
|
||||
pid_t pid = getpid();
|
||||
#endif
|
||||
spdlog::info("ThreadPool Worker : {}", pid);
|
||||
while (!terminate_) {
|
||||
std::unique_lock<std::mutex> lock(jobQueueMutex);
|
||||
spdlog::info("ThreadPool Worker : {} Waiting for a job", pid);
|
||||
|
||||
@@ -23,7 +23,7 @@ public:
|
||||
}
|
||||
#elif __linux__
|
||||
static void* thread_func(void *param) {
|
||||
auto task = static_cast<std::function<void* ()>*>(param);
|
||||
auto task = static_cast<std::function<void()>*>(param);
|
||||
(*task)();
|
||||
delete task;
|
||||
return 0;
|
||||
@@ -36,15 +36,11 @@ public:
|
||||
Thread& operator=(Thread&) = delete;
|
||||
|
||||
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) {
|
||||
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)...>>) {
|
||||
returnValue = __f(std::move(__args)...);
|
||||
}
|
||||
else {
|
||||
__f(std::move(__args)...); // void 반환은 저장하지 않음
|
||||
}
|
||||
returnValuePtr = new std::invoke_result_t<_Callable, _Args...>(__f(std::move(__args)...));
|
||||
};
|
||||
std::function<void()> funcPtr = std::move(boundFunc);
|
||||
#ifdef _WIN32
|
||||
@@ -53,9 +49,39 @@ public:
|
||||
pthread_create(&handle_, NULL, thread_func, funcPtr);
|
||||
#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();
|
||||
|
||||
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();
|
||||
|
||||
private:
|
||||
@@ -64,7 +90,7 @@ private:
|
||||
#elif __linux__
|
||||
pthread_t handle_;
|
||||
#endif
|
||||
void* returnValue = nullptr;
|
||||
void* returnValuePtr = nullptr;
|
||||
bool detached = false;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user