普通任務線程池
阿新 • • 發佈:2017-12-13
線程池#include <strsafe.h>
TCHAR pOutBuf[1024] = {};
#define PRINTF(...) StringCchPrintfW(pOutBuf,1024,__VA_ARGS__); WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),pOutBuf,lstrlen(pOutBuf),NULL,NULL);
DWORD WINAPI ThreadPoolProc(LPVOID lparam)
{
long * p = (long *)lparam;
while (true)
{
Sleep(300);
CString str;
InterlockedIncrement(p);//原子相加
PRINTF(_T("當前線程ID:%d 輸出值:%d \n"), GetCurrentThreadId(),*p);
}
return 0;
}
int main()
{
int iWait = 0;
wscanf_s(_T("%d"), &iWait);
//創建兩個線程,和CreateThread相似
QueueUserWorkItem(ThreadPoolProc, &iWait, WT_EXECUTEDELETEWAIT);
QueueUserWorkItem(ThreadPoolProc, &iWait, WT_EXECUTEDELETEWAIT);
getchar();
getchar();
return 0;
}
普通任務線程池