zoukankan      html  css  js  c++  java
  • 线程池的使用

    #include "stdafx.h"

    #include <Windows.h>
    #include <iostream>
    #include <process.h>

    using namespace std;
    INT i;
    VOID CALLBACK Fun(PTP_CALLBACK_INSTANCE Instancd,PVOID Context,PTP_WORK Work){
        cout<<i++<<' ';
    }

    int main(){
     
      PTP_WORK pw;
      TP_CALLBACK_ENVIRON cbe;
      PTP_POOL pool;
      pool=CreateThreadpool(NULL);//创建线程池
      if(NULL==pool ){
        cout<<"createThreadpoll failed!"<<endl;

      return -1;
      }
      SetThreadpoolThreadMaximum(pool,100);
      SetThreadpoolThreadMinimum(pool,1);

      InitializeThreadpoolEnvironment(&cbe);
      SetThreadpoolCallbackPool(&cbe,pool);

     pw=CreateThreadpoolWork(Fun,NULL,&cbe);//创建工作项
      for(int i=0;i<10000;i++){
         
          SubmitThreadpoolWork(pw);//提交工作项  

          }

    WaitForThreadpoolWorkCallbacks(pw, FALSE);//等待工作结束

     CloseThreadpoolWork(pw);

       system("pause");
       return 0;
    }

    下面是一些函数的用法和步骤,摘自:http://www.cnblogs.com/wz19860913/articles/1274214.html

    PTP_POOL pThreadpool = CreateThreadpool(NULL); // 创建线程池

    // 设置线程池线程数量上下限

    SetThreadpoolThreadMinimum(pThreadpool, 2);

    SetThreadpoolThreadMaximum(pThreadpool, 10);

    // 初始化“回调函数环境”结构

    TP_CALLBACK_ENVIRON tcbe;

    InitializeThreadpoolEnvironment(&tcbe);

    // 将该回调函数环境结构与线程池相关联

    SetThreadpoolCallbackPool(&tcbe, pThreadpool);

    // 创建清理组

    PTP_CLEANUP_GROUP pTpcg= CreateThreadpoolCleanupGroup();

    // 将回调函数环境结构与清理组关联起来

    SetThreadpoolCallbackCleanupGroup(&tcbe, pTpcg, NULL);

    // 现在可以创建一些项,提交给线程池

    PTP_WORK pTpWork = CreateThreadpoolWork(, &tcbe);// 创建一个工作项

    SubmitThreadpoolWork(pTpWork); // 提交工作项

    PTP_TIMER pTpTimer = CreateThreadpoolTimer(, &tcbe);// 创建一个定时器项

    SetThreadpoolTimer(pTpTimer, ); // 提交定时器

    PTP_WAIT pTpWait = CreateThreadpoolWait(, &tcbe);// 创建一个等待项

    SetThreadpoolWait(pTpWait, ); // 提交等待项

    PTP_IO pTpIO = CreateThreadpoolIo(, &tcbe); // 创建一个IO项

    StartThreadpoolIo(pTpIO); // 开始执行IO项

    // 等待所有项完成

    CloseThreadpoolCleanupGroupMembers(pTpcg, FALSE, NULL);

    // 关闭各个项

    CloseThreadpoolWork(pTpWork);

    CloseThreadpoolTimer(pTpTimer);

    CloseThreadpoolWait(pTpWait);

    CloseThreadpoolIo(pTpIO);

    CloseThreadpoolCleanupGroup(pTpcg); // 关闭线程池清理组

    DestroyThreadpoolEnvironment(&tcbe); // 删除回调函数环境结构

    CloseThreadpool(pThreadpool); // 关闭线程池

  • 相关阅读:
    centos7安装rabbitmq 总结
    python第六十三天-- 第十一周作业
    python第六十一天,第六十二天 redis
    python第六十天-----RabbitMQ
    python第五十四天--第十周作业
    python第五十三天--进程,协程.select.异步I/O...
    python第五十二天---第九周作业 类 Fabric 主机管理程序
    python第五十一天----线程,Event,队列
    Python基础之-面向对象编程(引言)
    Python中的模块
  • 原文地址:https://www.cnblogs.com/duyy/p/3726127.html
Copyright © 2011-2022 走看看