Win32 multi-threaded program design learning notes (Chapter 2)

zhaozj2021-02-08  277

Win32 multi-threaded program design learning notes (Chapter 2)

"Win32 multi-threaded program design" this book also reads a lot, but I always feel that the impression is not deep; I think it should be noted, I can deepen my impression. The second is also available for easy access. J

First of all, I think the thread is not difficult (such a psychological hint will make me feel a relaxedity);

Why?

Similar to the process of calling functions; threads only use CreateThread's API to function

Package and generate a program that is simultaneously executed simultaneously to call the packaged function.

Handle hthread = CreateThread

LPSecurity_Attributes LPTHREADATT,

DWORD DWSTACKSIZE

LPTHREAD_START_ROUTINE LPFUN,

LPVOID LPPARETER,

DWORD DWCREATIONFLAGS,

LPDWORD LPTHREADID)

LPFUN: That is to point to the address of the encapsulated function.

LPParameter: That is to point to the parameters of the encapsulated function (no parameters fill in null)

Illustration:

CreateThread () In addition to LPFUN, LPParameter is extremely important, other parameters are used? Of course, it is useful. When you use it, you can check it, it is not, then set it to null.

When CreateThread () is called, thread core objects (if thread core objects have been generated, only the reference count of this core object is added 1; in addition, there are several core objects in the Win32 system), and in handle The way represents it. Then, it is obvious that it should be shut down with a closehandle () function after the thread is used.

Since the thread is an independent running program, how do the main program know the thread is completed? The answer is through the getExitCodet (Handle Hthread, LPDWORD LPEXITCODE) function. If the thread is over, the code ended ended is placed back in the LPEXITCode parameter. If the thread has not ended, the value of LPEXITCODE brought back is STILL_ACTIVE.

It is worth noting that getExitCodet (LPDWORD LPEXITCODE) can also bring back the returns of the thread function. LPEXITCODE is the return value of the function

example:

Thread function:

DWORD WINAPI Threadfun (lpvoid n)

{

Return 10;

}

int main ()

{

Hthrd = Createthread (null, threadfun); ....);

DWORD EXITCODE = 0;

FOR (;;)

{

GetExitcodethread (HTHRD, & EXIXIXIDE);

IF (EXITCODE == STILL_ACTIVE)

{

// Thread is still running

}

Else

{

Break;

}

}

// EXITCODE Saves the return value of the function 10

Printf ("Return Value of Thread Functions:% D / N", EXITCODE);

CloseHandle (HTHRD);

Return 0;

}

This chapter is probably so much, summarized:

* Generate a thread [CreateThread ()]

* Judging whether the thread ends [getExitcodet "]

* Close thread [CloseHandle ()] * Force exit thread [exitthread ()]

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

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

New Post(0)