임시저장(빌드 안됨) 졸려서 자러갈거야
This commit is contained in:
26
impl/Session/Session.cpp
Normal file
26
impl/Session/Session.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "Session/Session.hpp"
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
Session::Session() {
|
||||
}
|
||||
|
||||
Session::~Session() {
|
||||
}
|
||||
|
||||
bool Session::init() {
|
||||
return false;
|
||||
}
|
||||
|
||||
void Session::destruct() {
|
||||
}
|
||||
|
||||
int Session::send() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int Session::recv() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +1,30 @@
|
||||
#include "Utils/Thread.hpp"
|
||||
|
||||
namespace Chattr {
|
||||
Thread::~Thread() {
|
||||
if (!detached)
|
||||
join();
|
||||
|
||||
Thread::Thread(Thread&& other) noexcept {
|
||||
other.detach();
|
||||
}
|
||||
|
||||
void Thread::join() {
|
||||
Thread& Thread::operator=(Thread&& other) noexcept {
|
||||
other.detach();
|
||||
return *this;
|
||||
}
|
||||
|
||||
Thread::~Thread() {
|
||||
if (!detached) {
|
||||
spdlog::critical("There is not joined thread");
|
||||
std::exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void* Thread::join() {
|
||||
#ifdef _WIN32
|
||||
WaitForSingleObject(handle_, INFINITE);
|
||||
#elif __linux__
|
||||
pthread_join(handle_, NULL);
|
||||
pthread_join(handle_, returnValue);
|
||||
#endif
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
void Thread::detach() {
|
||||
|
||||
47
impl/Utils/ThreadPool.cpp
Normal file
47
impl/Utils/ThreadPool.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "Utils/ThreadPool.hpp"
|
||||
#include "precomp.hpp"
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
ThreadPool::ThreadPool(std::uint32_t numThreads) {
|
||||
workers_.reserve(numThreads);
|
||||
|
||||
while (numThreads--)
|
||||
workers_.push_back([this]() -> void* { return this->Worker(); });
|
||||
}
|
||||
|
||||
ThreadPool::~ThreadPool() {
|
||||
terminate_ = true;
|
||||
jobQueueCV_.notify_all();
|
||||
|
||||
for (auto& t : workers_)
|
||||
t.join();
|
||||
}
|
||||
|
||||
void* ThreadPool::Worker() {
|
||||
#ifdef _WIN32
|
||||
DWORD pid = GetCurrentProcessId();
|
||||
#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);
|
||||
jobQueueCV_.wait(lock, [this]() { return !this->jobs_.empty() || terminate_; });
|
||||
if (this->jobs_.empty())
|
||||
return nullptr;
|
||||
|
||||
auto jobPair = std::move(jobs_.front());
|
||||
jobs_.pop();
|
||||
lock.unlock();
|
||||
|
||||
spdlog::info("ThreadPool Worker : {} Executing a job", pid);
|
||||
auto job = jobPair.first();
|
||||
jobPair.second() = (*job)();
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user