哈哈,这个弄完了之后,下一步就到线程的挂起,恢复,切换,终止啦。。
代码:
1 #include <Windows.h> 2 #include <stdio.h> 3 4 #define MAX_THREADS 5 5 6 DWORD WINAPI ThreadProc(LPVOID); 7 8 typedef struct _THREAD_PARAM{ 9 DWORD i; 10 DWORD dwRandom; 11 DWORD dwData; 12 } THREAD_PARAM, *LPTHREAD_PARAM; 13 14 DWORD WINAPI ThreadProc(LPVOID lpParam) 15 { 16 LPTHREAD_PARAM pData; 17 pData = (LPTHREAD_PARAM)lpParam; 18 printf("TID = %u,\t Parameters = %u, %u, %u\n", 19 GetCurrentThreadId(), 20 pData->i, pData->dwRandom, pData->dwData); 21 HeapFree(GetProcessHeap(), 0, pData); 22 return 0; 23 } 24 25 void main() 26 { 27 LPTHREAD_PARAM pData; 28 DWORD dwThreadId[MAX_THREADS]; 29 HANDLE hThread[MAX_THREADS]; 30 int i; 31 32 for(i = 0; i < MAX_THREADS; i++) 33 { 34 pData = (LPTHREAD_PARAM)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 35 sizeof(THREAD_PARAM)); 36 if(pData == NULL) 37 { 38 printf("HeapAlloc error;\n"); 39 ExitProcess(2); 40 } 41 42 pData->i = i; 43 pData->dwRandom = rand(); 44 pData->dwData = 100; 45 46 hThread[i] = CreateThread( 47 NULL, 48 0, 49 ThreadProc, 50 pData, 51 0, 52 &dwThreadId[i]); 53 if(hThread[i] == NULL) 54 { 55 ExitProcess(i); 56 } 57 } 58 WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE); 59 for(i = 0; i < MAX_THREADS; i++) 60 { 61 CloseHandle(hThread[i]); 62 } 63 }