mirror of
https://github.com/ttttupup/wxhelper.git
synced 2025-02-22 16:14:56 +08:00
feat: 增加线程池1
This commit is contained in:
parent
4027c4afe6
commit
0b52a49155
41
src/thread_pool.cc
Normal file
41
src/thread_pool.cc
Normal file
@ -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
|
26
src/thread_pool.h
Normal file
26
src/thread_pool.h
Normal file
@ -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<ThreadPool>{
|
||||||
|
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
|
Loading…
x
Reference in New Issue
Block a user