임시저장
This commit is contained in:
@@ -42,6 +42,18 @@ int main() {
|
|||||||
|
|
||||||
|
|
||||||
Chattr::ThreadPool threadPool(3);
|
Chattr::ThreadPool threadPool(3);
|
||||||
|
/*int returnedIntager = 0;
|
||||||
|
int passvalue = 2;
|
||||||
|
threadPool.enqueueJob([](int& i){
|
||||||
|
spdlog::info("JobTest");
|
||||||
|
if (i == 2) {
|
||||||
|
i = 1;
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}, returnedIntager, std::ref(passvalue));
|
||||||
|
|
||||||
|
Sleep(1000);*/
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
spdlog::info("Waiting for connection...");
|
spdlog::info("Waiting for connection...");
|
||||||
@@ -49,8 +61,8 @@ int main() {
|
|||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,13 +32,12 @@ void* ThreadPool::Worker() {
|
|||||||
if (this->jobs_.empty())
|
if (this->jobs_.empty())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto jobPair = std::move(jobs_.front());
|
auto job = std::move(jobs_.front());
|
||||||
jobs_.pop();
|
jobs_.pop();
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
|
|
||||||
spdlog::info("ThreadPool Worker : {} Executing a job", pid);
|
spdlog::info("ThreadPool Worker : {} Executing a job", pid);
|
||||||
auto job = jobPair.first();
|
job();
|
||||||
jobPair.second() = (*job)();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ public:
|
|||||||
int recv();
|
int recv();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct std::vector<Chattr::TCPSocket> tcpSock_;
|
std::vector<Chattr::TCPSocket> tcpSock_;
|
||||||
struct std::vector<Chattr::Socket> udpSock_;
|
std::vector<Chattr::Socket> udpSock_;
|
||||||
struct Snowflake sessId_;
|
struct Snowflake sessId_;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -12,24 +12,18 @@
|
|||||||
|
|
||||||
namespace Chattr {
|
namespace Chattr {
|
||||||
|
|
||||||
template<typename F, typename... Args>
|
|
||||||
concept ReturnsVoidPtr = std::is_same_v<
|
|
||||||
std::invoke_result_t<F, Args...>,
|
|
||||||
void*
|
|
||||||
>;
|
|
||||||
|
|
||||||
class Thread {
|
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::function<void()>*>(param);
|
||||||
std::unique_ptr<std::function<void*()>> task(static_cast<std::function<void* ()>*>(param));
|
|
||||||
(*task)();
|
(*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::packaged_task<void*()>*>(param));
|
auto task = static_cast<std::function<void* ()>*>(param);
|
||||||
(*task)();
|
(*task)();
|
||||||
delete task;
|
delete task;
|
||||||
return 0;
|
return 0;
|
||||||
@@ -42,16 +36,19 @@ public:
|
|||||||
Thread& operator=(Thread&) = delete;
|
Thread& operator=(Thread&) = delete;
|
||||||
|
|
||||||
template<typename _Callable, typename... _Args>
|
template<typename _Callable, typename... _Args>
|
||||||
requires ReturnsVoidPtr<_Callable, _Args...> && (!std::is_same_v<std::decay_t<_Callable>, Thread>)
|
requires (!std::is_same_v<std::decay_t<_Callable>, Thread>)
|
||||||
Thread(_Callable&& __f, _Args&&... __args) {
|
Thread(_Callable&& __f, _Args&&... __args) {
|
||||||
auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable -> void* {
|
auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable {
|
||||||
void* ret = __f(std::move(__args)...);
|
if constexpr (!std::is_void_v<std::invoke_result_t<decltype(__f), decltype(__args)...>>) {
|
||||||
returnValue = ret;
|
returnValue = __f(std::move(__args)...);
|
||||||
return ret;
|
}
|
||||||
|
else {
|
||||||
|
__f(std::move(__args)...); // void 반환은 저장하지 않음
|
||||||
|
}
|
||||||
};
|
};
|
||||||
std::function<void* ()> funcPtr = std::move(boundFunc);
|
std::function<void()> funcPtr = 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, new std::function<void()>(std::move(funcPtr)), 0, nullptr);
|
||||||
#elif __linux__
|
#elif __linux__
|
||||||
pthread_create(&handle_, NULL, thread_func, funcPtr);
|
pthread_create(&handle_, NULL, thread_func, funcPtr);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -15,17 +15,35 @@ public:
|
|||||||
~ThreadPool();
|
~ThreadPool();
|
||||||
|
|
||||||
template<typename _Callable, typename... _Args>
|
template<typename _Callable, typename... _Args>
|
||||||
requires ReturnsVoidPtr<_Callable, _Args...> && (!std::is_same_v<std::decay_t<_Callable>, Thread>)
|
requires (!std::is_void_v<std::invoke_result_t<_Callable, _Args...>>)
|
||||||
int enqueueJob(_Callable&& __job, void* retVal, _Args&&... __args) {
|
int enqueueJob(_Callable&& __job, std::invoke_result_t<_Callable, _Args...>& retVal, _Args&&... __args) {
|
||||||
if (terminate_) {
|
if (terminate_) {
|
||||||
spdlog::error("Cannot run jobs on threads that terminating...");
|
spdlog::error("Cannot run jobs on threads that terminating...");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(jobQueueMutex);
|
std::lock_guard<std::mutex> lock(jobQueueMutex);
|
||||||
auto boundFunc = std::bind(std::forward<_Callable>(__job), std::forward<_Args>(__args)...);
|
auto boundFunc = [&retVal, __job = std::move(__job), ... __args = std::move(__args)]() mutable {
|
||||||
std::function<void*()> job(std::move(boundFunc));
|
retVal = __job(std::move(__args)...);
|
||||||
jobs_.push(job, retVal);
|
};
|
||||||
|
auto task = std::packaged_task<void()>(std::move(boundFunc));
|
||||||
|
jobs_.push(std::move(task));
|
||||||
|
jobQueueCV_.notify_one();
|
||||||
|
}
|
||||||
|
template<typename _Callable, typename... _Args>
|
||||||
|
requires std::is_void_v<std::invoke_result_t<_Callable, _Args...>>
|
||||||
|
int enqueueJob(_Callable&& __job, _Args&&... __args) {
|
||||||
|
if (terminate_) {
|
||||||
|
spdlog::error("Cannot run jobs on threads that terminating...");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(jobQueueMutex);
|
||||||
|
auto boundFunc = [__job = std::move(__job), ... __args = std::move(__args)]() mutable {
|
||||||
|
__job(std::move(__args)...);
|
||||||
|
};
|
||||||
|
auto task = std::packaged_task<void()>(std::move(boundFunc));
|
||||||
|
jobs_.push(std::move(task));
|
||||||
jobQueueCV_.notify_one();
|
jobQueueCV_.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,7 +52,7 @@ private:
|
|||||||
|
|
||||||
std::condition_variable jobQueueCV_;
|
std::condition_variable jobQueueCV_;
|
||||||
std::mutex jobQueueMutex;
|
std::mutex jobQueueMutex;
|
||||||
std::queue<std::pair<std::function<void*()>, void*>> jobs_;
|
std::queue<std::packaged_task<void()>> jobs_;
|
||||||
std::vector<Chattr::Thread> workers_;
|
std::vector<Chattr::Thread> workers_;
|
||||||
bool terminate_ = false;
|
bool terminate_ = false;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user