- #include <Windows.h>
- #include <tchar.h>
- #include <stdio.h>
- #define MAX_THREADS 5
- DWORD WINAPI ThreadProc(LPVOID lpParam);
- typedef struct _THREAD_PARAM{
- DWORD i;
- DWORD dwRandom;
- DWORD dwData;
- }THREAD_PARAM,*LPTHREAD_PARAM;
- DWORD WINAPI ThreadProc(LPVOID lpParam){
- LPTHREAD_PARAM lpData;
- lpData = (LPTHREAD_PARAM)lpParam;
- printf("TID = %u,\t Parameter = %u,%u,%u\n",GetCurrentThreadId(),lpData->i,lpData->dwRandom,lpData->dwData);
- HeapFree(GetProcessHeap(),0,lpData);
- return 0;
- }
- INT main(void){
- LPTHREAD_PARAM lpData;
- DWORD dwThreadId[MAX_THREADS];
- HANDLE hThread[MAX_THREADS];
- INT i;
- for(i=0;i<MAX_THREADS;i++){
- lpData = (LPTHREAD_PARAM)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(THREAD_PARAM));
- if(lpData == NULL){
- printf("HeapAlloc error;\n");
- ExitProcess(2);
- }
- lpData->i = i;
- lpData->dwData = 100;
- lpData->dwRandom = rand();
- hThread[i] = CreateThread(NULL,0,ThreadProc,lpData,0,&dwThreadId[i]);
- if(hThread[i] == NULL){
- ExitProcess(i);
- }
- }
- system("pause");
- return 0;
- }
本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/629320,如需转载请自行联系原作者