#define  _CRT_SECURE_NO_WARNINGS 
#include "iostream"
#include "windows.h"
using namespace std;

void NTAPI poolThreadFunc(
    _Inout_ PTP_CALLBACK_INSTANCE Instance,
    _Inout_opt_ PVOID Context)
{
    cout << GetCurrentThreadId() << endl;
}

void NTAPI poolThreadWork(
    _Inout_ PTP_CALLBACK_INSTANCE Instance,
    _Inout_opt_ PVOID Context,
    _Inout_ PTP_WORK Work)
{
    cout << GetCurrentThreadId() << endl;
}

int main()
{
    //创建线程池
//    PTP_POOL threadPool = CreateThreadpool(NULL);
//    SetThreadpoolThreadMinimum(threadPool, 1);
//    SetThreadpoolThreadMaximum(threadPool, 3);
    //初始化环境
    TP_CALLBACK_ENVIRON te;
    InitializeThreadpoolEnvironment(&te);
//    SetThreadpoolCallbackPool(&te, threadPool);
    //创建线程
    //TrySubmitThreadpoolCallback(poolThreadFunc, NULL, &te);  //单次工作提交,以异步的方式运行函数,一次性任务
    //TrySubmitThreadpoolCallback(poolThreadFunc, NULL, &te);
    //TrySubmitThreadpoolCallback(poolThreadFunc, NULL, &te);
    //TrySubmitThreadpoolCallback(poolThreadFunc, NULL, &te);
    
    //清理线程池的环境变量
//    DestroyThreadpoolEnvironment(&te);
    //关闭线程池
//    CloseThreadpool(threadPool);

//    SuspendThread();   //更改线程状态为悬挂
//    ResumeThread();    //恢复线程状态运行

    /*
    创建工作项
    */
    PTP_WORK pwk;
    pwk = CreateThreadpoolWork(poolThreadWork, NULL, &te);
     //提交工作项,可以提交多次
    SubmitThreadpoolWork(pwk);
    SubmitThreadpoolWork(pwk);
    //等待工作结束
    WaitForThreadpoolWorkCallbacks(pwk, false);
    //关闭工作对象
    CloseThreadpoolWork(pwk);
    
    system("pause");
    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2021-11-29
  • 2021-11-26
  • 2021-11-08
  • 2022-03-10
  • 2021-08-10
  • 2022-12-23
  • 2021-11-05
猜你喜欢
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2021-07-24
  • 2022-12-23
  • 2022-01-21
  • 2022-12-23
相关资源
相似解决方案