From 0b52a49155e2b3b385a9ab0c7e3adb859cac240e Mon Sep 17 00:00:00 2001 From: hugy <504650082@qq.com> Date: Sat, 20 May 2023 17:12:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=20=E5=A2=9E=E5=8A=A0=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=B1=A01?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/thread_pool.cc | 41 +++++++++++++++++++++++++++++++++++++++++ src/thread_pool.h | 26 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/thread_pool.cc create mode 100644 src/thread_pool.h diff --git a/src/thread_pool.cc b/src/thread_pool.cc new file mode 100644 index 0000000..55d1bb9 --- /dev/null +++ b/src/thread_pool.cc @@ -0,0 +1,41 @@ + +#include "pch.h" +#include "thread_pool.h" + +#include "Windows.h" + +namespace wxhelper { +ThreadPool::~ThreadPool() { + CloseThreadpoolCleanupGroupMembers(cleanup_group_, true, NULL); + CloseThreadpoolCleanupGroup(cleanup_group_); + CloseThreadpool(pool_); +} +bool ThreadPool::Create(unsigned long min, unsigned long max) { + InitializeThreadpoolEnvironment(&env_); + pool_ = CreateThreadpool(NULL); + if (NULL == pool_) { + return false; + } + SetThreadpoolThreadMaximum(pool_, max); + BOOL ret = SetThreadpoolThreadMinimum(pool_, min); + if (FALSE == ret) { + return false; + } + cleanup_group_ = CreateThreadpoolCleanupGroup(); + if (NULL == cleanup_group_) { + return false; + } + SetThreadpoolCallbackPool(&env_, pool_); + SetThreadpoolCallbackCleanupGroup(&env_, cleanup_group_, NULL); + return true; +} + +bool ThreadPool::AddWork(PTP_WORK_CALLBACK callback,PVOID opt) { + PTP_WORK work = CreateThreadpoolWork(callback, opt, &env_); + if (NULL == work) { + return false; + } + SubmitThreadpoolWork(work); + return true; +} +} // namespace wxhelper \ No newline at end of file diff --git a/src/thread_pool.h b/src/thread_pool.h new file mode 100644 index 0000000..3693c90 --- /dev/null +++ b/src/thread_pool.h @@ -0,0 +1,26 @@ +#ifndef WXHELPER_THREAD_POOL_H_ +#define WXHELPER_THREAD_POOL_H_ +#include "Windows.h" +#include "singleton.h" +namespace wxhelper { + +class ThreadPool :public Singleton{ + public: + ~ThreadPool(); + + bool Create(unsigned long min = 1, unsigned long max = 4); + + bool AddWork(PTP_WORK_CALLBACK callback,PVOID opt); + + private: + void operator=(const ThreadPool&) = delete; + void operator=(ThreadPool&&) = delete; + + PTP_POOL pool_; + PTP_CLEANUP_GROUP cleanup_group_; + TP_CALLBACK_ENVIRON env_; +}; + +} // namespace wxhelper + +#endif \ No newline at end of file