SA is right about there being differences in waiting for all the objects versus waiting for multiple subsets to get more than 64. The issues is not so much related to the example at hand but is a more important to understand.
The issue at hand has to do with Thread Objects, which are only signaled when the thread exits so there's no possibility of deadlocks in this example. However, when used for synchronization, the definition of "WaitForMultipleObjects" is that when the wait is satisfied, you are guaranteed that
all the objects were available and you successfully "locked" them all at once. If WaitForMultipleObjects cannot obtain all the objects at the same time, it does not change the state of the individual objects as that would lead to deadlocks. And while that guarantee can be made for each subset, it does not apply to the sum of the subsets (the total set) of objects.
In the case where you are waiting for all threads to finish, there is no difference between waiting for all the objects at once or waiting for each thread one at a time in a loop (other than the actual limit to the number of objects).
<pre lang="cs">void thread_wait_for_all()
{
for (int i=0; i<True_Maximum_Simultaneous_Threads; i++)
{
if (ThreadArray[i] != NULL)
{
WaitForSingleObject(ThreadArray[i], INFINITE);
CloseHandle(ThreadArray[i]);
ThreadArray[i] = NULL;
}
}
}