欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[Windows] Windows线程池

程序员文章站 2022-07-05 12:28:55
...

WaitAndWorkInThreadPool.h

#pragma once

#include <windows.h>
#include "common.h"

//Thread Pools
//https://docs.microsoft.com/zh-cn/windows/win32/procthread/thread-pools


//对RegisterWaitForSingleObject的包装,功能为 “等到内核对象后立刻新起线程执行指定的例程,这个线程是被注册到windows  thread pool 中的”


// 需要通过继承的方式实现,继承类必须实现WorkCB虚函数,使用起来灵活性差一点
// [当前不可用] 目前没想到好的方法让纯虚函数能够作为回调函数,后面有好的办法再实现
class WaitAndWorkInThreadPool_A
{
	DEFAULT_COPY(WaitAndWorkInThreadPool_A)

public:
	WaitAndWorkInThreadPool_A() = delete;
	explicit WaitAndWorkInThreadPool_A(const HANDLE& waitfor,PVOID param = NULL, DWORD flag = WT_EXECUTEDEFAULT, DWORD timeout = INFINITE);
	virtual ~WaitAndWorkInThreadPool_A();

protected:
	virtual VOID NTAPI WorkCB(PVOID p, BOOLEAN b) = 0;

private:
	HANDLE _h;
	PVOID _p;
	DWORD _f;
	DWORD _t;
};


// 通过指定回调函数指正实现,使用起来灵活性强
class WaitAndWorkInThreadPool_B final
{
	DEFAULT_COPY(WaitAndWorkInThreadPool_B)

public:
	WaitAndWorkInThreadPool_B() = delete;
	explicit WaitAndWorkInThreadPool_B(const HANDLE& waitfor, WAITORTIMERCALLBACK workcb ,PVOID param = NULL, DWORD flag = WT_EXECUTEDEFAULT, DWORD timeout = INFINITE);
	~WaitAndWorkInThreadPool_B();
};

WaitAndWorkInThreadPool.cpp

#include "WaitAndWorkInThreadPool.h"

WaitAndWorkInThreadPool_A::WaitAndWorkInThreadPool_A(const HANDLE& waitfor, PVOID param, DWORD flag, DWORD timeout)
	:_h(waitfor), _p(param) , _f(flag), _t(timeout)
{
	PHANDLE tmp;
	//RegisterWaitForSingleObject(tmp, _h, &WorkCB, _p, timeout, flag);
}

WaitAndWorkInThreadPool_A::~WaitAndWorkInThreadPool_A()
{
	
}

// ------------------------------------------------------------------------------------------------------------------

WaitAndWorkInThreadPool_B::WaitAndWorkInThreadPool_B(const HANDLE& waitfor, WAITORTIMERCALLBACK workcb, PVOID param , DWORD flag , DWORD timeout)
{
	PHANDLE tmp = NULL;
	RegisterWaitForSingleObject(tmp, waitfor, workcb, param, timeout, flag);
}

WaitAndWorkInThreadPool_B::~WaitAndWorkInThreadPool_B()
{

}

相关标签: Windows