onnxruntime/winml/test/concurrency/ThreadPool.cpp
Justin Chu 416dc2e84d
Fix clang-format comment indents on Windows for winml/ (#17144)
On Windows, clang-format has a bug when AlignTrailingComments.Kind is
set to `Leave`
(https://clang.llvm.org/docs/ClangFormatStyleOptions.html#aligntrailingcomments),
where it will keep adding indentation to comments after each formatting
runs.

This PR changes to always align comments so we do not hit the bug.

As a consequence of the options change we need to reformat some of the
files. Note that this option is aligned with the rest of the repository.
2023-08-14 23:50:14 -04:00

33 lines
991 B
C++

#include "testPch.h"
#include "ThreadPool.h"
#include <ctime>
ThreadPool::ThreadPool(unsigned int initial_pool_size) : m_destruct_pool(false), m_threads() {
for (unsigned int i = 0; i < initial_pool_size; i++) {
m_threads.emplace_back([this]() {
while (true) {
std::unique_lock<std::mutex> lock(m_mutex);
// thread listening for event and acquire lock if event triggered
m_cond_var.wait(lock, [this] { return m_destruct_pool || !m_work_queue.empty(); });
if (!m_work_queue.empty()) {
auto work = m_work_queue.front();
m_work_queue.pop();
lock.unlock();
work();
} else {
// Work queue is empty but lock acquired
// This means we are destructing the pool
break;
}
}
});
}
}
ThreadPool::~ThreadPool() {
m_destruct_pool = true;
m_cond_var.notify_all(); // notify destruction to threads
for (auto& thread : m_threads) {
thread.join();
}
}