以异步的方式调用
- #include "stdafx.h"
- #include <windows.h>
- #include <iostream>
- using namespace std;
-
- //线程池的回调函数
- VOID WINAPI ThreadPoolCallBack(PTP_CALLBACK_INSTANCE instance, PVOID param)
- {
- cout << "param:" << (int)param << "\tThread id = " << GetCurrentThreadId() << endl;
- Sleep(200); // 模拟一个任务时间为100毫秒的执行
- return;
- }
-
- DWORD GetNumOfProcess()// 获取CPU的核心数
- {
- SYSTEM_INFO sysinfo;
- GetSystemInfo(&sysinfo); // 获取操作系统信息
- return sysinfo.dwNumberOfProcessors;
- }
-
- int main()
- {
- PTP_POOL tPool;
- tPool = CreateThreadpool(NULL); // 创建一个线程池
-
- DWORD dwMaxThread = 3; // GetNumOfProcess() * 2 + 1;
- //设置线程池参数(线程池中的线程数)
- SetThreadpoolThreadMaximum(tPool, dwMaxThread); // 线程池中最多线程数
- SetThreadpoolThreadMinimum(tPool, 1); // 线程池中最少线程数
-
- TP_CALLBACK_ENVIRON tcEnv;
- InitializeThreadpoolEnvironment(&tcEnv); // 初始化线程池的回调环境
- SetThreadpoolCallbackPool(&tcEnv, tPool); // 给线程池分配回调环境
-
- cout << "线程池中的线程数为:" << dwMaxThread << endl << endl;
- //测试例子
- for (int i = 1;i < 20;i++)
- {
- // 向线程池中投递一个任务
- TrySubmitThreadpoolCallback(ThreadPoolCallBack, (PVOID)i, &tcEnv);
- }
-
- Sleep(100000);
- return 0;
- }
线程池的定时器
-
- int g_ncount = 9;
- bool g_bfinish = false;
- TCHAR g_sztext[100];
- #define id_dlg 0x0000ffff
- void CALLBACK timeoutcallback(PTP_CALLBACK_INSTANCE instance, PVOID context,PTP_TIMER time)
- {
- HWND hwnd = FindWindow(NULL,L"warning");
- if (hwnd != NULL)
- {
- if (g_ncount == 0)
- {
- g_ncount = 9;
- EndDialog(hwnd, IDOK);
- return;
- }
- }
-
- char text[128];
- TCHAR Name[128];
- sprintf(text, "you have %d seconds to respond", g_ncount--);
- MultiByteToWideChar(CP_ACP, 0, text, -1, Name, 100);
- SetDlgItemText(hwnd, id_dlg, Name);
- qDebug() << text;
- }
-
-
- {
- //每隔一段时间,计时器
- PTP_TIMER time = CreateThreadpoolTimer(timeoutcallback, NULL, NULL);
- ULARGE_INTEGER uistarttime;
- uistarttime.QuadPart = (LONGLONG)-(10000000);
- FILETIME fttime;
- fttime.dwHighDateTime = uistarttime.HighPart;
- fttime.dwLowDateTime = uistarttime.LowPart;
- SetThreadpoolTimer(time, &fttime,1000,0);
- g_ncount = 9;
- MessageBox(NULL,L"you have 10 seconds to respond", L"warning",MB_OK);
- WaitForThreadpoolTimerCallbacks(time, TRUE);
- CloseThreadpoolTimer(time);
- }
-
线程池内核对象
- HANDLE g_hEvent;
- VOID CALLBACK WaitFuncCallback(PTP_CALLBACK_INSTANCE Instance, PVOID Context, PTP_WAIT Wait, TP_WAIT_RESULT WaitResult)
- {
- char text[128];
- sprintf(text,"WaitFuncCallback is called, data = %d\n", *(int*)Context);
- qDebug() << text;
- ResetEvent(g_hEvent);
- Sleep(5000);
- }
-
- //内核对象
- g_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- INT dd = 5;
- PTP_WAIT pWait = CreateThreadpoolWait(WaitFuncCallback, &dd, NULL);
-
- SetThreadpoolWait(pWait, g_hEvent, NULL);//只有被触发或者超时,才调用WaitFuncCallback函数
- Sleep(1000);
- SetEvent(g_hEvent);//触发
- Sleep(200); //等待内核对象被触发,调用回调函数
-
- WaitForThreadpoolWaitCallbacks(pWait, FALSE);//等待函数,等WaitFuncCallback函数返回才执行下去
- CloseThreadpoolWait(pWait);
- CloseHandle(g_hEvent);
io内核对象
- //io操作完成才会被调用
- VOID CALLBACK IoCompletionCallback(PTP_CALLBACK_INSTANCE Instance, PVOID Context, PVOID Overlapped
- , ULONG IoResult, ULONG_PTR NumberOfBytesTransferred, PTP_IO Io)
- {
- int a = 1;
- if (IoResult == NO_ERROR)
- {
- qDebug() << " no error! " << "transfer num is " << NumberOfBytesTransferred;
- }
- else
- {
- qDebug() << "IoResult is" << IoResult << "transfer num is " << NumberOfBytesTransferred;
- }
- }
-
-
-
-
- HANDLE hTxtFile = CreateFile(L"test.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
- PTP_IO PIO = CreateThreadpoolIo(hTxtFile, IoCompletionCallback, nullptr, nullptr);
- StartThreadpoolIo(PIO);
- char buf[512] = { 0 };
- OVERLAPPED ol = { 0 };
- bool bret = ReadFile(hTxtFile, buf, 512, nullptr, &ol);
- WaitForThreadpoolIoCallbacks(PIO, FALSE);
- CloseThreadpoolIo(PIO);
- CloseHandle(hTxtFile);
线程的封装
- VOID WINAPI ThreadPoolCallBack(PTP_CALLBACK_INSTANCE instance, PVOID param)
- {
- qDebug() << " tThread id = " << GetCurrentThreadId() ;
- Sleep(100); // 模拟一个任务时间为100毫秒的执行
- HANDLE phand = (HANDLE*)param;
- SetEvent(phand);
- }
-
-
-
- //封装线程池
- PTP_POOL tPool;
- tPool = CreateThreadpool(NULL); // 创建一个线程池
- SetThreadpoolThreadMaximum(tPool, 4);//设置线程池的最大个数
- SetThreadpoolThreadMinimum(tPool, 1);
- TP_CALLBACK_ENVIRON tcenv;
- InitializeThreadpoolEnvironment(&tcenv); //设置线程池的环境资源对象
- SetThreadpoolCallbackPool(&tcenv, tPool); // 给线程池分配回调环境
-
- HANDLE hEvent[10];
- for (int i = 0; i < 10; i++)
- {
- hEvent[i] = CreateEvent(NULL, FALSE, FALSE, NULL);
- TrySubmitThreadpoolCallback(ThreadPoolCallBack, hEvent[i],&tcenv);// 向线程池中投递一个任务,并且线程立即执行
- }
- WaitForMultipleObjects(10, hEvent, TRUE, INFINITE);
-
- DestroyThreadpoolEnvironment(&tcenv);//将线程池的环境资源对象释放
- CloseThreadpool(tPool);//关闭线程池句柄,来优化系统资源