Win32 multi-threaded programming learning notes (Chapter 3)

zhaozj2021-02-08  201

Win32 multi-threaded programming learning notes (Chapter 3)

By learning in the previous chapter, I learned to use the getExitCodetRead () function to determine if a thread is still executing. Through this function, I can deal with "A thread must run after a thread". Review:

Program fragment:

FOR (;;)

{

Int rc;

Rc = getexitcodethread (hthrd, & exitcode);

IF (! RC && EXITCODE! = STILL_ACTIVE)

Break;

}

Using this method, you must continue to call getExitCodetThread () until the result is no longer STILL_ACTIVE. On the book, this method is not good, very waste of CPU time., Called Busy Waits. Busy waiting must pay attention to, or you will find that the reactions of other ongoing programs are very dull after running the program containing busy waiting. Because the CPU has almost all available time to check if the thread is end.

So what should I do? How can it be more efficient?

Another efficient method:

Use WaitForsingleObject () this function to solve the problem.

Completing the above program fragment The same function is only one sentence:

WaitforsingleObject (hthrd, infinite);

If the HTHRD thread does not end, the program that calls this function will stop in the function call point until the HTHRD thread ends (the term: HTHRD is excited) will continue.

I don't know if everyone will have such questions ------ This function will not only package the above program clip? Is it very efficient? If it is efficient, how is it doing?

Can tell you, WaitForsingleObject () is not a simple packaging of the above-mentioned segments, it is really efficient (do not believe, press the method, open the performance manager), as for how it does, I am also interested in knowing ( Know that I only know this function gets the support of certain underlying scheduling functions of the operating system level.

Basically this chapter is described above. (As for the WaitForsingleObject () parameter and the return value of the return value, check the book, no need to say)

Specifically look at a real example, better than this long story, please see:

// Reference book example

// Program, only 3 threads, complete 6 things

int main ()

{

Handle Hthrds [3];

INT Slot = 0;

For (INT i = 1; i <= 6; i )

{

IF (i> 3)

{// already exists 3 threads

// Wait for one of the thread harness, create a thread to do the remaining things

// efficiency is not high, because the order of thread ends is different from they produced

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

For (slot = 0; slot <3; slot )

{

// Waiting for the remaining thread end

WaitforsingleObject (hthrds [slot], infinite); CloseHandle (HTHRDS [slot]);

} // end for

}

The above program has a problem, that is, the efficiency is not very high; the ideal situation is that once there is a thread end, it will immediately generate a thread. Read the above program carefully, you will find that it cannot achieve the ideal situation; because it assumes the order of the thread to end the same order as they produce. For example, after sequential generation of 1, 2, 3 threads, it must be ended in the order of 1, 2, 3. In fact, maybe 2 to 1 earlier. At this time, the above program does not immediately generate a thread fill 2, but not to wait for the end, only threads. So can you achieve the ideal situation? The answer is OK, please use the waitformultipleObjects () function, this function is basically almost like WaitForsingleObject (), here I don't exempt it.

This chapter finally refers to MsgwaitFormultiPleObjects (), which is characterized by returning at the end of the message reaches or thread.

I think, as long as it will use the WaitForsingleObject () function, WaitFormultiPleObjects () and msgwaitformultipleObjects () will use it (two small examples to explain, look at how to spend, don't have to be Here is nonsense).

This chapter is probably so much, summarized:

* Waiting for thread end [WaitForsingleObject ()]]

* Waiting for multiple threads to end [WaitFormultipleObjects ()]

* Wait for multiple threads or messages to reach [MSGWaitFormultipleObjects ()]

Note: The above text is purely to strengthen memory, content or unknown, or even mistakes, please forgive me, hurry !!!

转载请注明原文地址:https://www.9cbs.com/read-3144.html

New Post(0)