By studying in the previous chapter, I learned to use the getexitcodethread () function to determine whether a thread is still being executed. With this function, I can deal with"
A thread can run only after a thread ends.. Review:
Program snippet:
For (;;)
{
Int RC;
Rc = getexitcodethread (hthrd, & exitcode );
If (! RC & exitcode! = Still_active)
Break;
}To use this method, you must continuously call getexitcodethread () until the result is no longer still_active.
The book says this method is not good.Is a waste of CPU time.
Busy waiting (busy waits ).Waiting for the CPU to spend almost all the available time to check whether the thread is finished.You must pay attention to it. Otherwise, you will find thatBusy waitingAfter the program is running, the response of other ongoing programs will be very slow. BecauseSo what should I do? How can we make it more efficient?
Another efficient method:Use the waitforsingleobject () function to solve the problem perfectly. You only need one sentence to complete the same function of the above program fragment:
Waitforsingleobject (hthrd, infinite );If the hthrd thread does not end, the program that calls this function will stop at the function call point until the hthrd thread ends (term: hthrd is fired. I wonder if you will have such a question ------ this function won't just wrap the above program fragment? Is it really efficient? If it is efficient, how does it do it? We can tell you that waitforsingleobject () is not a simple package of the above program fragment, it is really efficient (do not believe it, follow the method in the book, open the Performance Manager to see), as to how it does it, I am also interested in knowing (know and tell me). I only know that this function is supported by some underlying scheduling functions at the operating system level. Basically, the core content of this chapter is described above. (As
Waitforsingleobject ()The description of parameters and returned values can be found in the book. You do not need to talk about them more.) A specific example is better than a long article here. For more information, see:
//
Reference examples
//
Program purpose: only 3
Threads,
Completion 6
Things
Int main ()
{
Handle hthrds [3];
Int slot = 0;
For (INT I = 1; I <= 6; I ++)
{
If (I> 3)
{//
3 already exists
Threads
//
Wait for a thread harness, and then create a thread to do the rest.
//
Low efficiency, because the thread end order is different from the order in which they are generated
Waitforsingleobject (hthrds [Slot], infinite );
Closehandle (hthrds [Slot]);
}
//
Construct thread
Hthrds [Slot] = createthread (null, 0, threadfunc, null, 0, null );
If (++ slot> 2)
Slot = 0;
} // End
For (slot = 0; slot <3; slot ++)
{
//
Wait until the remaining thread ends
Waitforsingleobject (hthrds [Slot], infinite );
Closehandle (hthrds [Slot]);
} // End
}
The above program has a problem, that is, the efficiency is not very high; ideally, once a thread ends, a thread is immediately generated to complete. By carefully reading the above program, you will find that it cannot achieve the ideal situation, because it assumes that the thread end order will be the same as the order in which they are generated. For example, after threads 1, 2, and 3 are generated in sequence, they must end in the order of 1, 2, and 3. In fact, 2 May end earlier than 1. At this time, the above program will not immediately generate a thread to fill 2, but it will have to wait until 1 ends to generate a thread. So can we achieve the ideal situation? The answer is yes. Please use
Waitformultipleobjects ()Function. The usage of this function is basically the same
Waitforsingleobject ()I will not give an example here. This chapter also mentions
Msgwaitformultipleobjects ()This function is used to return messages when the message arrives or the thread ends. I think, you only need to use it.
Waitforsingleobject ()This function
Waitformultipleobjects ()And
Msgwaitformultipleobjects ()It will also be used for a look (two small examples are provided in the book to illustrate that it will not take much time, so I don't have to talk nonsense here ). The content of this chapter is probably so much. To sum up:
*
Wait for the thread to end [waitforsingleobject ()]
*
Wait for multiple threads to end [waitformultipleobjects ()]
*
Wait until multiple threads end or the message arrives at [msgwaitformultipleobjects ()]