mirror of
https://github.com/taurusxin/ncmdump.git
synced 2025-09-14 13:56:49 +08:00
Update thread_pool and logger
This commit is contained in:
parent
e3aedd510f
commit
27b560c67e
@ -17,8 +17,17 @@ class Logger
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Logger():
|
Logger():
|
||||||
is_running_(true)
|
is_running_(false) {}
|
||||||
|
|
||||||
|
~Logger()
|
||||||
{
|
{
|
||||||
|
if (is_running_)
|
||||||
|
join();
|
||||||
|
}
|
||||||
|
|
||||||
|
void start()
|
||||||
|
{
|
||||||
|
is_running_ = true;
|
||||||
thread_ = std::thread([&]() {
|
thread_ = std::thread([&]() {
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
@ -36,34 +45,28 @@ public:
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
~Logger()
|
|
||||||
{
|
|
||||||
if (is_running_)
|
|
||||||
join();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void cerr(Args... args)
|
void cerr(Args&&... args)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mutex_);
|
std::unique_lock<std::mutex> lock(mutex_);
|
||||||
msg_queue_.push(std::bind([](auto... args) {
|
msg_queue_.push(std::bind([](auto... args) {
|
||||||
std::cerr << generateTimeFormatString();
|
std::cerr << generateTimeFormatString();
|
||||||
((std::cerr << args), ...);
|
((std::cerr << args), ...);
|
||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
}, std::move(args)...));
|
}, std::forward<Args>(args)...));
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
cv_.notify_all();
|
cv_.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void cout(Args... args)
|
void cout(Args&&... args)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mutex_);
|
std::unique_lock<std::mutex> lock(mutex_);
|
||||||
msg_queue_.push(std::bind([](auto... args) {
|
msg_queue_.push(std::bind([](auto... args) {
|
||||||
std::cout << generateTimeFormatString();
|
std::cout << generateTimeFormatString();
|
||||||
((std::cout << args), ...);
|
((std::cout << args), ...);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}, std::move(args)...));
|
}, std::forward<Args>(args)...));
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
cv_.notify_all();
|
cv_.notify_all();
|
||||||
}
|
}
|
||||||
|
@ -16,8 +16,17 @@ class ThreadPool
|
|||||||
public:
|
public:
|
||||||
ThreadPool():
|
ThreadPool():
|
||||||
thread_num_(std::thread::hardware_concurrency() * 2),
|
thread_num_(std::thread::hardware_concurrency() * 2),
|
||||||
is_running_(true)
|
is_running_(false) {}
|
||||||
|
|
||||||
|
~ThreadPool() noexcept
|
||||||
{
|
{
|
||||||
|
if (is_running_)
|
||||||
|
join();
|
||||||
|
}
|
||||||
|
|
||||||
|
void start()
|
||||||
|
{
|
||||||
|
is_running_ = true;
|
||||||
work_threads_ = std::make_unique<std::thread[]>(thread_num_);
|
work_threads_ = std::make_unique<std::thread[]>(thread_num_);
|
||||||
for (unsigned int i = 0; i < thread_num_; ++i)
|
for (unsigned int i = 0; i < thread_num_; ++i)
|
||||||
{
|
{
|
||||||
@ -40,12 +49,6 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
~ThreadPool() noexcept
|
|
||||||
{
|
|
||||||
if (is_running_)
|
|
||||||
join();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Func, class... Args>
|
template<class Func, class... Args>
|
||||||
void submit(Func&& func, Args&&... args)
|
void submit(Func&& func, Args&&... args)
|
||||||
{
|
{
|
||||||
|
@ -45,7 +45,7 @@ void processFileTask(fs::path filePath, fs::path outputFolder)
|
|||||||
}
|
}
|
||||||
catch (const std::invalid_argument &e)
|
catch (const std::invalid_argument &e)
|
||||||
{
|
{
|
||||||
logger.cerr(BOLDRED, "[Exception] ", RESET, RED, e.what(), RESET, " '", filePath.u8string(), "'");
|
logger.cerr(BOLDRED, "[Exception] ", RESET, RED, std::string(e.what()), RESET, " '", filePath.u8string(), "'");
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
@ -198,6 +198,8 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
logger.start();
|
||||||
|
thread_pool.start();
|
||||||
if (thread_pool.joinable())
|
if (thread_pool.joinable())
|
||||||
thread_pool.join();
|
thread_pool.join();
|
||||||
if (logger.joinable())
|
if (logger.joinable())
|
||||||
@ -226,6 +228,8 @@ int main(int argc, char **argv)
|
|||||||
processFile(filePathU8, "");
|
processFile(filePathU8, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
logger.start();
|
||||||
|
thread_pool.start();
|
||||||
if (thread_pool.joinable())
|
if (thread_pool.joinable())
|
||||||
thread_pool.join();
|
thread_pool.join();
|
||||||
if (logger.joinable())
|
if (logger.joinable())
|
||||||
|
Loading…
Reference in New Issue
Block a user