Reputation: 8593
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.
Upvotes: 2
Views: 1400
Reputation: 18429
Seems you are stuck while debugging. When you step-over CreateThread
it may not immediately create and run the thread, since you are debugging at the point.
Remember, while actively debugging the code, only one thread is allowed to run, all other threads remain suspended. When you hit F10 or F11, the debugger gives debuggee to run, which is eventually asks OS to run process/threads to execute. I may add more, but your question demands more clarification.
Upvotes: 2
Reputation: 68631
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 WaitForSingleObject
before any of them have had a chance to do any work.
Upvotes: 7
Reputation: 613282
The threads do in fact all start immediately. You can write to any data structure from the threads so long as you correctly synchronize access to that structure.
Upvotes: 4