sn0w
12.12.2009, 19:06
вообщем простая подмена (не идеальная конечно) для WaitForMultipleObjects (когда речь идет об n потоках, причем n > MAXIMUM_WAIT_OBJECTS) и нам нужно ждать когда все они завершатся.
#include "stdafx.h"
#include <conio.h>
#include <windows.h>
DWORD g_dwThreadsCompleted;
CRITICAL_SECTION g_cs;
//////////////////////////////////////////////////////////////////////////
DWORD WINAPI SampleThread(LPVOID lp)
{
Sleep(1000);
EnterCriticalSection(&g_cs);
g_dwThreadsCompleted++;
LeaveCriticalSection(&g_cs);
printf("thread #%d completed\n", (int)lp);
return 0;
}
//////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
InitializeCriticalSection(&g_cs);
g_dwThreadsCompleted = 0;
HANDLE threads[100];
DWORD num_threads = 100; // sizeof(threads)/sizeof(HANDLE)
DWORD tid;
printf("spawning and awaiting threads to be completed!\n\n");
for(int i=0; i<num_threads; i++)
threads[i] = CreateThread(0,0,SampleThread,(LPVOID)i,0,&tid);
// now wait for the threads will be completed
while(g_dwThreadsCompleted<num_threads)
Sleep(500);
printf("wait completed\n");
return getch();
}
#include "stdafx.h"
#include <conio.h>
#include <windows.h>
DWORD g_dwThreadsCompleted;
CRITICAL_SECTION g_cs;
//////////////////////////////////////////////////////////////////////////
DWORD WINAPI SampleThread(LPVOID lp)
{
Sleep(1000);
EnterCriticalSection(&g_cs);
g_dwThreadsCompleted++;
LeaveCriticalSection(&g_cs);
printf("thread #%d completed\n", (int)lp);
return 0;
}
//////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
InitializeCriticalSection(&g_cs);
g_dwThreadsCompleted = 0;
HANDLE threads[100];
DWORD num_threads = 100; // sizeof(threads)/sizeof(HANDLE)
DWORD tid;
printf("spawning and awaiting threads to be completed!\n\n");
for(int i=0; i<num_threads; i++)
threads[i] = CreateThread(0,0,SampleThread,(LPVOID)i,0,&tid);
// now wait for the threads will be completed
while(g_dwThreadsCompleted<num_threads)
Sleep(500);
printf("wait completed\n");
return getch();
}