zoukankan      html  css  js  c++  java
  • 线程池之在内核对象触发时调用一个对象

    步骤:

      1、创建一个类似于void CALLBACK WaitCallBack( PTP_CALLBACK_INSTANCE pInstance, PVOID pvContext, PTP_WAIT pcbe, TP_WAIT_RESULT WaitResult)的函数

      2、创建一个内核对象

      3、CreateThreadpoolWait,创建等待

      4、SetThreadpoolWait,设置等待

      5、CloseThreadpoolWait、关闭内核对象

    注意:

      1、一次SetThreadpoolWait,一次调用回调函数

    代码:

      

    #include <iostream>
    #include <afx.h>
    using namespace std;
    
    HANDLE g_hEvent;
    
    void CALLBACK WaitCallBack(
    	PTP_CALLBACK_INSTANCE pInstance,
    	PVOID pvContext,
    	PTP_WAIT pcbe,
    	TP_WAIT_RESULT WaitResult)
    {
    	cout << "线程ID:" << GetCurrentThreadId() << endl;
    	ResetEvent(g_hEvent);
    }
    
    void main()
    {
    	g_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    	PTP_WAIT pWait = CreateThreadpoolWait(WaitCallBack, NULL, NULL);
    
    	SetThreadpoolWait(pWait, g_hEvent, NULL);
    	SetEvent(g_hEvent);
    
    	Sleep(200);
    
    	SetThreadpoolWait(pWait, g_hEvent, NULL);
    	SetEvent(g_hEvent);
    	Sleep(200);
    
    	WaitForThreadpoolWaitCallbacks(pWait, FALSE);
    
    	CloseThreadpoolWait(pWait);
    	CloseHandle(g_hEvent);
    }
    

     结果:

      

  • 相关阅读:
    20201016---不做清单
    20201014--增删改查
    20201013--什么是真实的自己?
    多态
    继承
    关键字
    分类思想
    常用的linux命令
    九九乘法表
    稀疏数组
  • 原文地址:https://www.cnblogs.com/wang-can/p/3340624.html
Copyright © 2011-2022 走看看