Related concepts of windows thread

Related concepts of windows thread

thread

A process consists of two parts, one is the process kernel object and the other is the address space. Likewise, a
thread is made up of two parts:

  • One is the thread's kernel object, which the operating system uses to manage the thread. Kernel objects are also where the system uses to store thread statistics.

  • The other is the thread stack, which is used to maintain all the function parameters and local variables that the thread needs to execute the code

Since threads require less overhead than processes, you should always try to solve programming problems by adding more threads instead of creating new processes. However, this advice is not set in stone. Many program designs are better implemented with multiple processes.

When to create a thread

Threads are used to describe the running paths in a process. Whenever a process is initialized, the system creates a main thread. The thread starts running with the C/C++ runtime library's startup code, which calls the entry point function (main, wmain, WinMain, or wWinMain), and continues to run until the entry point function returns and the C/C++ runs until the startup code of the library calls ExitProcess. For many applications, this main thread is the only thread the application needs. However, processes can create more threads to help perform their operations.

When can't create a thread

While the advantages of multithreaded applications are many, there are also some disadvantages. Multithreading should be used with caution. Don't use it if you want. Many very useful and powerful applications can be written using only the main thread given to the process.

write thread function

Each thread must have an entry point function from which the thread starts running. The entry point function for the main thread has been described earlier: main, wmain, WinMain, or wWinMain. If you want to create a worker thread in your process, it must also be an entry point function.

The following is a description of several problems with thread functions:

  • Unlike the main thread's entry point function, which must be named main, wmain, Win Main, or w Win Main, thread functions can have any name. In fact, if you have multiple thread functions in your application, you must give them different names, otherwise the compiler/linker will think you've created multiple implementations for a single function.
  • Since you pass string arguments to your main thread's entry point functions, you can use the ANSI/Unicode versions of the
    entry point functions: main/wmain and Win Main/w Win Main. You can pass a single parameter to a thread function, and the meaning of the parameter is defined by you, not by the operating system. So don't worry about ANSI/U Unicode issues.
  • The thread function must return a value, which will be the exit code for that thread. This is similar to the C/C++ runtime library's principle of having the main thread's exit code be the process's exit code.
  • Thread functions (actually all your functions) should use function parameters and local variables whenever possible. When using static and global variables, multiple threads can access these variables at the same time, which can corrupt the contents of the variables. However, parameters and local variables are created on the thread stack, so they are less likely to be destroyed by another thread.

CreateThread function

If you want to create one or more helper functions, just have an already running thread call CreateThread:

HANDLE CreateThread(
    LPSECURITY_ATTRIBUTES lpThreadAttributes,//SD
    SIZE_T dwStackSize,//initialstacksize
    LPTHREAD_START_ROUTINE lpStartAddress,//threadfunction
    LPVOID lpParameter,//threadargument
    DWORD dwCreationFlags,//creationoption
    LPDWORD lpThreadId//threadidentifier
);

When Create Thread is called, the system creates a thread kernel object. The thread kernel object is not the thread itself, but a smaller data structure used by the operating system to manage the thread. A thread kernel object can be thought of as a small data structure consisting of statistics about a thread. This is the same relationship as between processes and process kernel objects.

The system allocates memory from the process' address space for use by the thread's stack. The new thread runs in the same process environment that the thread was created in. Therefore, the new thread has access to all handles to the kernel objects of the process, all memory in the process, and the stacks of all other threads in this same process. This makes it really easy for multiple threads in a single process to communicate with each other.

terminate the running of the thread

To terminate the running of the thread, you can use the following methods:
• The thread function returns (this is the best method).
• By calling the Exit T thread function, the thread will automatically undo (preferably do not use this method).
• A thread in the same process or another process calls the Terminate Thread function (this method should be avoided).
• Terminate the process containing the thread (this method should be avoided).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325400533&siteId=291194637