There is a CreateThread() call within a for loop, and I want all the threads to be launched one after another and each thread writes data to a object.
`$while (ii != mapOServs.end())
{
Array_of_Thread_Handles[i] = CreateThread(NULL,0,MyThread, &args[i] , 0 , NULL);
}
But the threads don’t start until it hits WaitForMultipleObjects or WaitForSingleObject.
How do I make all the threads run one after another without waiting for a response?
Also, what is the best object to use so that it can be modified by different threads at the same time.
The threads are indeed “started” immediately — Windows will create the necessary internal structures, allocate the stack and so forth, and add them to the scheduler’s run list. However, they won’t necessarily be scheduled immediately.
In particular, the thread doing the launching will likely keep running until it has used up its time slice. If you have more threads running than processor cores (including threads in other processes), then your new threads may well not be scheduled on a processor for a while, and your thread doing the launching may execute up to a synchronization call such as
WaitForSingleObjectbefore any of them have had a chance to do any work.