리눅스에서 테스트 해볼 것

This commit is contained in:
2025-04-30 15:11:33 +09:00
parent cebd6bb9e9
commit cf8166f41c
2 changed files with 11 additions and 9 deletions

View File

@@ -59,10 +59,12 @@ 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();
Sleep(10000000);
} }
} }

View File

@@ -16,14 +16,14 @@ class Thread {
public: public:
#ifdef _WIN32 #ifdef _WIN32
static unsigned __stdcall thread_func(LPVOID param) { static unsigned __stdcall thread_func(LPVOID param) {
auto task = static_cast<std::function<void()>*>(param); auto task = static_cast<std::packaged_task<void()>*>(param);
(*task)(); (*task)();
delete task; delete task;
return 0; return 0;
} }
#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::packaged_task<void()>*>(param);
(*task)(); (*task)();
delete task; delete task;
return 0; return 0;
@@ -42,27 +42,27 @@ public:
auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable { auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable {
returnValuePtr = new std::invoke_result_t<_Callable, _Args...>(__f(std::move(__args)...)); returnValuePtr = new std::invoke_result_t<_Callable, _Args...>(__f(std::move(__args)...));
}; };
std::function<void()> funcPtr = std::move(boundFunc); std::packaged_task<void()>* funcPtr = new std::packaged_task<void()>(std::move(boundFunc));
#ifdef _WIN32 #ifdef _WIN32
handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, new std::function<void()>(std::move(funcPtr)), 0, nullptr); handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, funcPtr, 0, nullptr);
#elif __linux__ #elif __linux__
pthread_create(&handle_, NULL, thread_func, funcPtr); pthread_create(&handle_, NULL, thread_func, funcPtr);
#endif #endif
} }
/*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...>> 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 {
__f(std::move(__args)...); __f(std::move(__args)...);
}; };
std::function<void()> funcPtr = std::move(boundFunc); std::packaged_task<void()>* funcPtr = new std::packaged_task<void()>(std::move(boundFunc));
#ifdef _WIN32 #ifdef _WIN32
handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, new std::function<void()>(std::move(funcPtr)), 0, nullptr); handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, funcPtr, 0, nullptr);
#elif __linux__ #elif __linux__
pthread_create(&handle_, NULL, thread_func, funcPtr); pthread_create(&handle_, NULL, thread_func, funcPtr);
#endif #endif
}*/ }
~Thread(); ~Thread();
void join() { void join() {