스레드 재수정
This commit is contained in:
10
Server/include/ServerManager/ServerManager.hpp
Normal file
10
Server/include/ServerManager/ServerManager.hpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace Chattr {
|
||||||
|
|
||||||
|
class ServerManager {
|
||||||
|
public:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
0
Server/src/ServerManager/ServerManager.cpp
Normal file
0
Server/src/ServerManager/ServerManager.cpp
Normal file
@@ -5,7 +5,6 @@
|
|||||||
#include "Utils/Thread.hpp"
|
#include "Utils/Thread.hpp"
|
||||||
#include "Utils/ThreadPool.hpp"
|
#include "Utils/ThreadPool.hpp"
|
||||||
#include "Utils/StringTokenizer.hpp"
|
#include "Utils/StringTokenizer.hpp"
|
||||||
#include "Session/Session.hpp"
|
|
||||||
|
|
||||||
#include "precomp.hpp"
|
#include "precomp.hpp"
|
||||||
|
|
||||||
@@ -41,8 +40,8 @@ int main() {
|
|||||||
spdlog::info("PID : {}", pid);
|
spdlog::info("PID : {}", pid);
|
||||||
|
|
||||||
|
|
||||||
Chattr::ThreadPool threadPool(3);
|
Chattr::ThreadPool threadPool(0);
|
||||||
/*int returnedIntager = 0;
|
int returnedIntager = 0;
|
||||||
int passvalue = 2;
|
int passvalue = 2;
|
||||||
threadPool.enqueueJob([](int& i){
|
threadPool.enqueueJob([](int& i){
|
||||||
spdlog::info("JobTest");
|
spdlog::info("JobTest");
|
||||||
@@ -53,7 +52,7 @@ int main() {
|
|||||||
return 1;
|
return 1;
|
||||||
}, returnedIntager, std::ref(passvalue));
|
}, returnedIntager, std::ref(passvalue));
|
||||||
|
|
||||||
Sleep(1000);*/
|
sleep(10);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
spdlog::info("Waiting for connection...");
|
spdlog::info("Waiting for connection...");
|
||||||
|
|||||||
@@ -4,10 +4,20 @@
|
|||||||
namespace Chattr {
|
namespace Chattr {
|
||||||
|
|
||||||
ThreadPool::ThreadPool(std::uint32_t numThreads) {
|
ThreadPool::ThreadPool(std::uint32_t numThreads) {
|
||||||
workers_.reserve(numThreads);
|
int numCPU = numThreads;
|
||||||
|
if (numThreads == 0) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
SYSTEM_INFO sysinfo;
|
||||||
|
GetSystemInfo(&sysinfo);
|
||||||
|
numCPU = sysinfo.dwNumberOfProcessors;
|
||||||
|
#elif __linux__
|
||||||
|
numCPU = sysconf(_SC_NPROCESSORS_ONLN);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
workers_.reserve(numCPU);
|
||||||
|
|
||||||
while (numThreads--)
|
while (numCPU--)
|
||||||
workers_.push_back([this]() -> int { this->Worker(); return 1; });
|
workers_.push_back([this]() { this->Worker(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
ThreadPool::~ThreadPool() {
|
ThreadPool::~ThreadPool() {
|
||||||
@@ -24,9 +34,9 @@ void* ThreadPool::Worker() {
|
|||||||
#elif __linux__
|
#elif __linux__
|
||||||
pthread_t pid = pthread_self();
|
pthread_t pid = pthread_self();
|
||||||
#endif
|
#endif
|
||||||
|
spdlog::info("ThreadPool Worker : {} up.", pid);
|
||||||
while (!terminate_) {
|
while (!terminate_) {
|
||||||
std::unique_lock<std::mutex> lock(jobQueueMutex);
|
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_; });
|
jobQueueCV_.wait(lock, [this]() { return !this->jobs_.empty() || terminate_; });
|
||||||
if (this->jobs_.empty())
|
if (this->jobs_.empty())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ public:
|
|||||||
auto task = std::packaged_task<void()>(std::move(boundFunc));
|
auto task = std::packaged_task<void()>(std::move(boundFunc));
|
||||||
jobs_.push(std::move(task));
|
jobs_.push(std::move(task));
|
||||||
jobQueueCV_.notify_one();
|
jobQueueCV_.notify_one();
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
template<typename _Callable, typename... _Args>
|
template<typename _Callable, typename... _Args>
|
||||||
requires std::is_void_v<std::invoke_result_t<_Callable, _Args...>>
|
requires std::is_void_v<std::invoke_result_t<_Callable, _Args...>>
|
||||||
|
|||||||
Reference in New Issue
Block a user