Windows core programming Reading Notes-Chapter 6 thread basics, Chapter 6 Reading Notes

Source: Internet
Author: User
Tags microsoft c

Windows core programming Reading Notes-Chapter 6 thread basics, Chapter 6 Reading Notes

1. processes use more system resources than threads. The reason is the address space. Creating a virtual address space for a process requires a large amount of system resources. A thread has only one kernel object and one stack.

2. Thread entry functions

DWORD WINAPI ThreadFunc(PVOID pvParam){DWORD dwResult = 0;...return(dwResult);}

Description of thread Functions

  • The thread function can be named at will.
  • A thread function has only one parameter, which is defined by us rather than the operating system. Therefore, we do not have to worry about ANSI/Unicode issues.
  • A thread function must return a value that will become the exit code of the thread.
  • Thread functions should use function parameters and local variables whenever possible. Function parameters and local variables are created on the thread stack.

3. CreateThread Function

HANDLE CreateThread(PSECURITY_ATTRIBUTES psa,DWORD cbStackSize,PTHREAD_START_ROUTINE pfnStartAddr,PVOID pvParam,DWORD dwCreateFlags,PDWORD pdwThreadID);

Call the CreateThread function. The system allocates memory from the address space of the process to the thread stack. The new thread runs in the same process context as the thread in charge of creation. Therefore,The new thread can access all handles of the Process kernel objects, all memory in the process, and stacks of all other threads in the same process..

Note: The CreateThread function is a Windows function used to create a thread. However, if you write C/C ++ code, do not call CreateThread. On the contrary, the correct choice is to use the Microsoft C ++ Runtime library function._ Beginthreadex.

(1) psa parameters

If you want to use the default security attribute of the thread kernel object, you can input NULL to this parameter.

(2) cbStackSize Parameter

The cbStackSize parameter specifies the address space that a thread can use for its thread stack. When the CreateProcess function starts a process, it calls CreateThread internally to initialize the main thread of the process. For the cbStackSize parameter, CreateProcess uses a value stored in the executable file.The capacity of the reserved address space is set to the upper limit of the stack space, so as to capture infinite recursive bugs in the code.

(3) pfnStartAddr and pvParam Parameters

The pfnStartAddr parameter specifies the address of the thread function to be executed by the new thread. The pvParam parameter of the thread function is the same as that of the pvParam parameter of the CreateThread function.

(4) dwCreateFlags Parameters

The dwCreateFlags parameter specifies an additional flag to control the creation of a thread.

(5) pdwThreadID Parameter

This parameter must be a valid address of DWORD. The CreateThread function uses it to store the ID assigned to the new thread by the system.

4. Terminate the running thread

The thread can terminate the operation in the following four ways:

Thread function return (this is strongly implemented ).

The thread "kills" itself by calling the ExitThread function.

The TerminateThread function is called by a thread in the same process or another process.

Process containing threads stops running (this method is not recommended)

5.

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.