1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
|
#include<windows.h> #include<strsafe.h>//win2003SDK必须安装 要不无此头文件。此文件是为了实现StringCchPrintf,StringCchLength。 #define MAX_THREADS3 #define BUF_SIZE255 typedef struct _MyData{ int val1; int val2; }MYDATA,*PMYDATA; DWORDWINAPIThreadProc(LPVOIDlpParam) { HANDLEhStdout; PMYDATApData; TCHARmsgBuf[BUF_SIZE]; size_tcchStringSize; DWORDdwChars; hStdout=GetStdHandle(STD_OUTPUT_HANDLE); if (hStdout==INVALID_HANDLE_VALUE) return1; pData=(PMYDATA)lpParam; StringCchPrintf(msgBuf,BUF_SIZE,TEXT( "Parameters=%d,%d\n" ), pData->val1,pData->val2); StringCchLength(msgBuf,BUF_SIZE,&cchStringSize); WriteConsole(hStdout,msgBuf,cchStringSize,&dwChars,NULL); HeapFree(GetProcessHeap(),0,pData); return0; } voidmain() { PMYDATApData; DWORDdwThreadId[MAX_THREADS]; HANDLEhThread[MAX_THREADS]; inti; for (i=0;i<MAX_THREADS;i++) { pData=(PMYDATA)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, sizeof (MYDATA)); if (pData==NULL) ExitProcess(2); pData->val1=i; pData->val2=i+100; hThread[i]=CreateThread( NULL, 0, ThreadProc, pData, 0, &dwThreadId[i]); if (hThread[i]==NULL) { ExitProcess(i); } } WaitForMultipleObjects(MAX_THREADS,hThread,TRUE,INFINITE); for (i=0;i<MAX_THREADS;i++) { CloseHandle(hThread[i]); } } |