onnxruntime/winml/test/concurrency/ThreadPool.h
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
1,008 B
C++

#pragma once
#include <vector>
#include <thread>
#include <queue>
#include <mutex>
#include <future>
class ThreadPool {
private:
std::condition_variable m_cond_var;
bool m_destruct_pool;
std::mutex m_mutex;
std::vector<std::thread> m_threads;
std::queue<std::function<void()>> m_work_queue;
public:
ThreadPool(unsigned int initial_pool_size);
~ThreadPool();
template <typename F, typename... Args>
inline auto SubmitWork(F&& f, Args&&... args) -> std::future<decltype(f(args...))> {
auto func = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
auto task = std::make_shared<std::packaged_task<decltype(f(args...))()>>(std::forward<decltype(func)>(func));
{
std::lock_guard<std::mutex> lock(m_mutex);
// wrap packed task into a void return function type so that it can be stored in queue
m_work_queue.push([task]() { (*task)(); });
}
m_cond_var.notify_one(); // unblocks one of the waiting threads
return task->get_future();
}
};