From aaaf706febdef3db4f9991066ac429dc6067fd80 Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Wed, 23 Apr 2025 07:13:43 +0900 Subject: [PATCH] =?UTF-8?q?Thread=20=EC=9D=BC=EB=B6=80=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/src/server.cpp | 6 ++++++ impl/Utils/Thread.cpp | 2 ++ include/Utils/Thread.hpp | 32 +++++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 impl/Utils/Thread.cpp diff --git a/Server/src/server.cpp b/Server/src/server.cpp index dfb5d4d..3974b0b 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -1,6 +1,8 @@ #include "Socket/TCPSocket.hpp" #include "Socket/Log.hpp" #include "Utils/ConfigManager.hpp" +#include "Utils/Thread.hpp" + #include "precomp.hpp" int main() { @@ -37,6 +39,10 @@ int main() { else clientAddr.length = sizeof(sockaddr_in6); + Chattr::Thread t1([]() { + spdlog::info("thread?"); + }); + spdlog::info("Waiting for connection..."); sock.accept(clientSock, clientAddr); } \ No newline at end of file diff --git a/impl/Utils/Thread.cpp b/impl/Utils/Thread.cpp new file mode 100644 index 0000000..679ebac --- /dev/null +++ b/impl/Utils/Thread.cpp @@ -0,0 +1,2 @@ +#include "Utils/Thread.hpp" + diff --git a/include/Utils/Thread.hpp b/include/Utils/Thread.hpp index 9fa2554..8866ea9 100644 --- a/include/Utils/Thread.hpp +++ b/include/Utils/Thread.hpp @@ -5,4 +5,34 @@ #else #error "이 플랫폼은 지원되지 않습니다." -#endif \ No newline at end of file +#endif +#include + +namespace Chattr { + + +class Thread { +public: +#ifdef _WIN32 + static DWORD WINAPI __thread(LPVOID param) { + std::unique_ptr> func(reinterpret_cast*>(param)); + (*func)(); + return 0; + } +#endif + template + Thread(Callable&& f, Args&&... args); +}; + +template +Thread::Thread(Callable&& f, Args&&... args) { + auto boundFunc = std::bind(std::forward(f), std::forward(args)...); + + auto funcPtr = new std::function(boundFunc); + +#ifdef _WIN32 + CreateThread(nullptr, 0, __thread, funcPtr, 0, nullptr); +#endif +} + +} // namespace Chattr \ No newline at end of file