Multi-thread programming (1) -- createthread

Source: Internet
Author: User

The following is a prototype of the sdk api createthread provided by the operating system:

HANDLE WINAPI CreateThread(
__in LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in SIZE_T dwStackSize,
__in LPTHREAD_START_ROUTINE lpStartAddress,
__in LPVOID lpParameter,
__in DWORD dwCreationFlags,
__out LPDWORD lpThreadId
);

Lpthreadattributes:
Pointer to the security_attributes type structure. Ignore this parameter in Windows 98. In Windows NT, if it is set to null, the default value is used.

Dwstacksize:
Thread stack size, usually 0. In any case, Windows dynamically prolongs the stack size as needed. You can use the/stack of the linker.:[Reserve
] [, Commit]


To control.

Reserve sets the number of address spaces reserved by the system for the thread stack. The default value is 1 MB. Commit indicates the amount of physical memory space allocated for the address space originally expected by the stack. The default value is 1 page.

Lpstartaddress:
Pointer to a thread function, in the form of @ function name. The function name is not limited, but must be declared in the following column:

DWORD winapi threadproc (lpvoid pparam)
If the format is incorrect, the call will fail.

Lpparameter
: The parameter passed to the thread function. It is a pointer to the structure and is null when no parameter is required.

Dwcreationflags
: If it is set to create_suincluded, the thread is suspended after being created, only when you callResumethread
It continues.

If it is set to 0, it will be executed immediately after the thread is created.

If it is set to stack_size_param_is_a_reservation, useDwstacksize
Mark the reserve, otherwise it is commit.

InWindows 2000/NT and Windows ME/98/95
Stack_size_param_is_a_reservation is invalid.

Lpthreadid
: Save the ID of the new thread.

Function return value:
If the function succeeds, the thread handle is returned. If the function fails, false is returned.

The following is an example:

Sdkthreading. h

# Ifndef multithread <br/> # define multithread <br/> class drawpicture <br/> {<br/> Public: <br/> static DWORD winapi mythreadfunc (lpvoid lpparam ); </P> <p> void draw (); <br/> PRIVATE: <br/> static int npoint; <br/>}; <br/> # endif

Sdkthreading. cpp

# Include <iostream> <br/> # include <windows. h> <br/> # include "sdkthreading. H "<br/> using namespace STD; <br/> int drawpicture: npoint = 20; <br/> DWORD winapi drawpicture: mythreadfunc (lpvoid lpparam) <br/>{< br/> while (npoint> 0) <br/>{< br/> cout <"It is point: "<npoint -- <Endl; <br/>}< br/> return 0; <br/>}< br/> void drawpicture: Draw () <br/>{< br/> handle hthread; <br/> hthread = createthread (null, 0, mythreadfunc, null, 0, null); <br/> If (hthread! = NULL) <br/>{< br/> closehandle (hthread); <br/>}< br/> int main (void) <br/>{< br/> drawpicture * pdraw = new drawpicture; <br/> pdraw-> draw (); <br/> Delete pdraw; <br/> system ("pause"); <br/> return 0; <br/>}< br/>

There are four main types of thread termination:

1. The return of the thread function. This method is strongly recommended.

2. The thread calls the exitthread function to commit suicide (callback ~~ It is difficult to stop using it)

3. Another thread calls the terminatethread function ~ This method should also be avoided)

4. Force termination of processes containing threads (this method should also be avoided)

This is the best way to handle the return of a thread function. If a thread function returns normally, it will handle the following four tasks:

1. All the C ++ objects created in the thread functions are destructed normally through their destructor.

2. The operating system correctly releases the memory used by the thread stack.

3. The operating system sets the exit code of the thread as the return value of the thread function.

4. The system reduces the usage count of the thread kernel object.

When the thread is terminated, it will handle the following tasks:

1. All user object handles owned by the thread will be released. In Windows, most objects are owned by processes that contain the thread for creating these objects. However, a thread has two user objects: Windows and hooks. When a thread ends, the system will automatically destroy any windows created or installed by the thread, and uninstall any hooks created or installed by the thread. Other objects will be destroyed only when the thread-owning process is terminated.

2. the exit code of the thread changes from still_active to the Code passed to exitthread or terminatethread.

3. The state of the thread kernel object changes to the trigger state.

4. If the thread is the last active process of the process, the system determines that the process is terminated.

5. Reduce the usage count of the thread kernel object to 1.

You can use getexitcodethread to determine whether a thread is terminated. For more information, see msdn.

 

A detailed description of the _ beginthreadex and afxbeginthread functions of the C ++ and MFC libraries is provided later.

 

 

Download sample code in this series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.