Summary of learning experience of C and C++ multi threading

  • 2020-04-02 01:05:26
  • OfStack

I think it is best to understand the relationship between the process and the thread before learning multi-threaded programming, and then in the process of learning how to work in the thread to write a (I started from the copy) multi-threaded small program, will be very helpful to learn multi-threading, otherwise only the theory is very abstract.

Before learning multithreaded programming, you must first know what is the thread function, thread function is the entrance to another thread function. By default a we have written code is only one thread, and the function is the main entrance to the thread () function, which is the default system. And we create another thread also needs a function to enter, this function is called a thread function.

In C/C++, you can call the 'runtime library' function _beginthreadex(...) To create a thread, _beginthreadex(...) The function accepts 6 parameters, among which the third parameter requires the address of the entry function of the thread to be passed in (that is, the function name of the thread function to be passed in +&), the meaning of every other parameter can be looked up in MSDN or on the Internet, the introduction of the rest of the parameters can be all passed 0.

_beginthreadex (...). Function returns a handle, which is the handle of the new thread. There is a requirement for the incoming thread function. The thread function must return type unsigned and accept a void* argument.
First, a piece of multithreaded simple code:

#include<windows.h>
#include<process.h>
#include<iostream>
using namespace std;
bool stop;
unsigned Counter;
unsigned __stdcall thread(void*)
{
cout <<"In second thread..." <<endl;
while (!stop){
Sleep(200);
cout <<Counter++ <<" " <<flush;
}
//_endthreadex(0);
return 0;
}
int main()
{
HANDLE hThread;
unsigned int threadID;
stop = false;
cout <<"Creating second thread..." <<endl;
// Create the second thread.
hThread = (HANDLE)_beginthreadex(NULL, 0, &thread, NULL, 0, &threadID);
// Wait until second thread terminates. If you comment out the line
// below, Counter will not be correct because the thread has not
// terminated, and Counter most likely has not been incremented to
// 1000000 yet.
//WaitForSingleObject(hThread, INFINITE);
system("pause");
stop = true;
//cin >>stop;
cout <<"Counter is-> " <<Counter <<endl;
// Destroy the thread object.
CloseHandle(hThread);
system("pause");
return 0;
} 

The code is correct, copy and paste it into Visual C++ 6.0 to compile and run, but you will find _beginthreadex(...) Function undefined compilation error. This is because our visual c++ 6.0 default is to program in single-threaded mode, if you need to multithreaded programming, need to convert the compiler's 'runtime library', the method is very simple:
The Project - > Settings - > In C/C++, select Code generation in the Category, and then select one of the multithreads in the Use run-time library.
At this point, compile again, you can pass. With this example of the function, it should have been started. But there is a lot of other basic knowledge to be covered, it is recommended to read it several times < Windows core programming (4th edition) > Chapter 6.
In fact, under different compilation environments, _beginthreadex(...) The functions may have different names, but they are all against the Windows function CreateThread(...). Encapsulation of CreateThread(...) The CreateThread(...) function is used to create a new thread function, CreateThread(...). The function also takes 6 arguments, under conditions like _beginthreadex(...) While the CreateThread function can be used to create a new thread, _beginthreadex(...) is strongly recommended. Function for the CreateThread function < Windows core programming (4th edition) > There is a good explanation in chapter 6.
When a thread's task ends, there are four ways to exit:
1. Return of thread function (this method is better);
2. By calling the _endthreadex() or ExitThread() functions, the thread will undo itself (preferably not);
3. The TerminateThread() function is called by a thread in the same process or another process (this method should be avoided);
4. Processes that contain threads terminate (this method should be avoided).
Through its best entry function return statement (i.e., return) to exit the thread, can also by calling the C/C + + 'runtime libraries' function _endthreadex exit () function, there are two ways of exit, forced exit are similar. It is best to use the thread function return statement (return) to exit the threads, only in this way can safe recovery of the thread processor resources and memory resources. But in fact _endthreadex () function and _beginthreadex (...). The function encapsulates the Windows function ExitThread(). If you must force the thread to exit, it is strongly recommended to call the _endthreadex() function to safely recover system resources.

Specific _beginthreadex (...). The _endthreadex() function and the _endthreadex() function < Windows core programming (4th edition) > Chapter 6 has a good dissection. There are also two _beginthread(...) The _endthread() runtime library functions are also covered.
Windows also provides library functions to get a HANDLE to the current process or thread, such as the HANDLE GetCurrentProcess() function that returns a HANDLE to the current process, and the HANDLE GetCurrentThread() function that returns a HANDLE to the current thread.

If you need to use the handle of another thread or process externally, you can use DuplicateHandle(...) The DuplicateHandle(...) function, which takes 7 arguments, can be used in MSDN and network. After the handle obtained by the function, you need to close the handle using the CloseHandle() function.

Related articles: