Windows systems provide us with the relevant APIs that we can use for multithreaded programming.
To create a thread's function:
HANDLE CreateThread( // SD SIZE_T dwStackSize, // initial stack size LPTHREAD_START_ROUTINE lpStartAddress, // thread function LPVOID lpParameter, // thread argument DWORD dwCreationFlags, // creation option LPDWORD lpThreadId // thread identifier);
The simplest C + + multi-threaded:
#include <iostream>#include <windows.h>using namespace STD;D Word WINAPI Fun (lpvoid lpparameter) { while(1) {cout<<"Fun display!"<<endl; Sleep ( +); }}intMain () {HANDLE hthread = CreateThread (NULL,0, Fun, NULL,0, NULL); CloseHandle (Hthread); while(1) {cout<<"Main display!"<<endl; Sleep ( +); }return 0;}
Running the above program will find some time to output a newline, sometimes there is no output line wrapping, and sometimes output two line break. This is because multithreaded programs run concurrently, and if some resources are shared among multiple threads, there is no guarantee that these resources will be properly exploited because the resources are not exclusive.
The improved program is as follows:
#include <iostream>#include <windows.h>using namespace STD; HANDLE Hmutex;dword WINAPI Fun (lpvoid lpparameter) { while(1) {WaitForSingleObject (Hmutex, INFINITE);cout<<"Fun display!"<<endl; Sleep ( +); ReleaseMutex (Hmutex); }}intMain () {HANDLE hthread = CreateThread (NULL,0, Fun, NULL,0, NULL); Hmutex = CreateMutex (NULL,false,"Screen"); CloseHandle (Hthread); while(1) {WaitForSingleObject (Hmutex, INFINITE);cout<<"Main display!"<<endl; Sleep ( +); ReleaseMutex (Hmutex); }return 0;}
WIN32 provides a series of API functions to complete thread creation, suspend, resume, terminate, and communicate.
CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId)
The function creates a new thread in the process space of its calling process and returns a handle to the built thread, with the parameters described below:
- Lpthreadattributes: A pointer to a security_attributes structure that determines the security attributes of a thread and is generally NULL;
- Dwstacksize: Specifies the thread's stack depth, which is generally set to 0;
- Lpstartaddress: Represents the address of the function that the code is in when the new thread starts executing, that is, the starting address of the thread. The general situation is (Lpthread_start_routine) Threadfunc,threadfunc is the name of the thread function;
- Lpparameter: Specifies the 32-bit parameter that the thread performs simultaneous to the thread, which is the parameter of the thread function;
- dwCreationFlags: To control the additional flags created by the thread, you can take two kinds of values. If the parameter is 0, the thread starts executing immediately after it is created, and if the parameter is create_suspended, the thread is suspended after the system generates the thread, and is not executed immediately until the function resumethread is called;
- Lpthreadid: This parameter returns the ID of the thread that was created, or returns a handle to the thread if the creation was successful, otherwise returns NULL.
SuspendThread(HANDLE hThread)
The function is used to suspend the specified thread, and if the function executes successfully, the execution of the thread is terminated.
ResumeThread(HANDLE hThread)
This function is used to end a thread's pending state and execute the thread.
ExitThread(DWORD dwExitCode)
This function is used for the execution of the thread termination itself, which is called in the execution function of the main threads. Where parameter dwexitcode is used to set the thread's exit code.
BOOL
In general, the thread function returns normally after the thread has finished running, but the application can invoke TerminateThread to forcibly terminate the execution of a particular thread. Each parameter has the following meanings:
- Hthread: The handle of the thread that will be terminated;
- Dwexitcode: The exit code used to specify the thread.
Terminating execution of a thread with TerminateThread () is unsafe and may cause system instability, although the function terminates the execution of the thread immediately, but does not release the resources that the thread occupies. Therefore, it is generally not recommended to use this function.
BOOL PostThreadMessage( DWORD idThread, UINT Msg, WPARAM wParam,
The function places a message into the message queue of the specified thread and returns without waiting for the message to be processed by the thread.
- Idthread: The ID of the thread that will receive the message;
- Msg: Specifies the message to be sent;
- WParam: Word parameters related to the message;
- LParam: Long parameters related to the message;
When this function is called, the function fails to execute if the thread that is about to receive the message does not create a message loop.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + multithreaded programming notes