概述:如何在 windows 下不使用 C++ 标准库函数创建多线程并等待其执行状态
如下所示为一段创建多线程并等待执行完成的 demo。也可参考官方示例,使用 createthread 创建,创建线程 - Win32 apps | Microsoft Learn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| int nCount = g_tOracleUser.size(); HANDLE *pHandles = (HANDLE *)malloc(sizeof(HANDLE) * g_tOracleUser.size()); if (pHandles) { int iHandleIndex = 0; for (auto & user : g_tOracleUser) { unsigned int dwThreadID = 0; pHandles[iHandleIndex] = (HANDLE)_beginthreadex(NULL, 0, (_beginthreadex_proc_type)&_DoCheckThread, (LPVOID)user.GetString(), 0, &dwThreadID); ATLTRACE(L"[%s] _beginthreadex _SendDataSyncThread , dwThreadID : %u", __FUNCTIONW__, dwThreadID); iHandleIndex++; }
WaitForMultipleObjects(nCount, pHandles, TRUE, INFINITE); ATLTRACE(L"[%s] all check finish...count : %d", __FUNCTIONW__, g_outRiskUsers.size()); tRiskUsers = g_outRiskUsers; } else { ATLTRACE(L"[%s] malloc handle failed!!!", __FUNCTIONW__); }
|