win32 – WaitForMultipleObjects的使用

创建5个线程,并无限期地打印某些内容

#include <Windows.h>#include <stdio.h>DWORD IDs[5];DWORD WINAPI ThreadProc(LPVOID TID) { //expected this block to run infinitely. while (1) { printf("Inside Thread: %d. \n", TID); Sleep(1000); } return 0;}#define NBOFTHREADS 5int main() { HANDLE lpHandles[NBOFTHREADS]; for (int i = 0; i < NBOFTHREADS; i++) { HANDLE Threadhandle = CreateThread(0, 0, ThreadProc, &IDs[i], 0, &IDs[i]); printf("Thread %d -> ID %d started. \n", i, IDs[i]); lpHandles[i] = Threadhandle; } WaitForMultipleObjects(NBOFTHREADS, lpHandles, TRUE, INFINITE); return 0;}

 

相关文章