Threading Overview
Understanding Windows Kernel objects
A thread is one of the system kernel objects. Before learning a thread, you should understand the kernel object first. A kernel object is a block of memory allocated by the system kernel that describes a data structure whose members are responsible for maintaining various information about the object. Kernel object data can only be accessed by the system kernel, and applications cannot find these data structures in memory and directly change their content.
Common system kernel objects include event objects, file objects, job objects, mutex objects, pipe objects, process objects, and thread objects. Different types of kernel objects have different data structures. Understanding processes and Threads
A process is considered to be an instance of a running program, and it is also part of the system kernel object. A process can be simply understood as a container, it simply provides space, and the code that executes the program is implemented by threads. Threads exist in the process and are responsible for executing code in the process address space. When a process is created, a thread is automatically created for it, which is called the main thread. In the main thread, the user can create other threads through code, and the process ends when the main thread in the process ends. Creation of Threads
There are several ways to create threads under Windows, as described below. Notice the difference between them. To create a thread using the CreateThread function
Windows API functions. This function creates a new thread on the basis of the main thread. Microsoft provides a function CreateThread to create a new thread in the Windows API.
Handlecreatethread (
lpsecurity_attributes lpthreadattributes,//Thread Security property
DWORD dwstacksize,//stack size
Lpthread_start_routine lpstartaddress,//thread function
lpvoid lpparameter,//thread parameters
DWORD dwcreationflags,//Thread Create attribute
lpdword lpthreadid//thread ID
);
The code example is as follows:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
using namespace std;
DWORD WINAPI Fun1proc (lpvoid lpparameter)
{
cout << "thread function fun1proc!\n";
return 0;
}
int main ()
{
HANDLE hThread1 = CreateThread (null, 0, fun1proc, NULL, 0, NULL);
CloseHandle (HTHREAD1);
Sleep (1000);
cout << "Main end!\n";
System ("pause");
return 0;
}
Run Result:
To create a thread using the _beginthreadex function
In addition to creating threads using the CreateThread API function, you can create threads using the _beginthreadex function provided in the C + + language.
uintptr_t _beginthreadex (///NATIVE CODE
void *security,// Thread safety Property
unsigned stack_size, //thread stack size
unsigned (*start_address) (void *),//thread function
void *arglist, //passed to parameters in the thread function
unsigned initflag, //thread initialization token
unsigned *thrdaddr //thread ID
code example:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <process.h>
using namespace std;
unsigned int _stdcall threadproc (lpvoid lpparameter)
{
cout << "thread function threadproc!\n";
return 0;
}
int main ()
{
_beginthreadex (null, 0, ThreadProc, 0, 0, null);
Sleep (1000);
cout << "Main end!\n";
System ("pause");
return 0;
}
To create a thread using the AfxBeginThread function
In an MFC application, you can also use the AfxBeginThread function to create a thread. MFC provides two overloaded versions of AfxBeginThread, one for user interface threads and another for worker threads. Only worker threads are spoken here.
cwinthread* AfxBeginThread (Afx_threadproc pfnthreadproc,
lpvoid lParam,
int npriority = Thread_priority_ NORMAL,
UINT nstacksize = 0,
DWORD dwcreateflags = 0,
lpsecurity_attributes lpsecurityattrs = Null
);//used to create worker threads
Return value: Returns a pointer to a thread object that points to a new thread if successful, otherwise null.
Pfnthreadproc: The entry function of the thread, the declaration must be as follows: UINT mythreadfunction (LPVOID pparam), cannot be set to null;
Pparam: Pass in the parameter of the thread, note its type is: LPVOID, so we can pass a struct body into the thread.
Npriority: The priority of the thread, typically set to 0. Let it have the same priority as the primary thread.
Nstacksize: Specifies the size of the stack for the newly created thread. If 0, the newly created thread has the same size stack as the main thread
Dwcreateflags: Specifies how the line Chengyu after the thread is created. You can specify two values: create_suspended: After the thread is created, it is suspended until the ResumeThread 0 is called: The thread is created and starts running.
Lpsecurityattrs: Point to a security_attributes structure that flags the security of the newly created thread. If NULL, the newly created thread has the same security as the main thread.
Code slightly. Other ways
The creation thread can also be created with the Boost line threading, or it can be created using the line threading (Std::thread) in the c++11 new standard. It is very convenient to use line threading in c++11 to create threads. And Cross-platform, is the language level. Later you will learn to use the C++11 line threading.