Reentrant function Reentrant function is mainly used in multitasking environment. A reentrant function is simply a function that can be interrupted, that is, it can be interrupted at any time when it is executed, and it can be transferred to OS scheduling to execute another section of code, and there will be no error when returning control; while non-reentrant functions use some system resources, such as global variable area, interrupt vector table, etc., so if it is interrupted, there may be problems. Such functions cannot run in a multitasking environment. Reentrant function can also be understood in this way. Reentry means repeated entry. First, it means that this function can be interrupted. Secondly, it means that it does not rely on any environment (including static) except using the variables on its own stack. Such a function is purecode (pure code) reentrant, which allows multiple copies of the function to run. Since they use separate stacks, they will not interfere with each other. If you really need to access global variables (including static), you must pay attention to the implementation of mutual exclusion. Reentrant functions are very important in a parallel execution environment, but generally they have to pay some performance cost for accessing global variables. Functions that meet the following conditions are mostly non-reentrant: 1) Static data structures are used in the function body; 2) Malloc() or free() functions are called in the function body; 3) Standard I/O functions are called in the function body. The following example illustrates this. Reentrant function void strcpy(char *lpszDest, char *lpszSrc) { while(*lpszDest++=*lpszSrc++); *dest=0; } Non-reentrant function 1 char cTemp;//global variables void SwapChar1(char *lpcX, char *lpcY) { cTemp=*lpcX; *lpcX=*lpcY; lpcY=cTemp;//accessed global variables } Write a reentrant function? Do not access global variables in the function body, do not use static local variables, and stick to using local variables. The function you write will be reentrant. If you must access global variables, remember to use mutex semaphores to protect global variables. Rewrite a non-reentrant function into a reentrant function? The only way to make a non-reentrant function reentrant is to rewrite it using reentrant rules. As long as you follow a few easy-to-understand rules, the function you write will be reentrant. 1) Do not use global variables. Because other code is likely to overwrite the values of these variables. 2) When interacting with hardware, remember to perform operations such as disinterrupt(), which is to turn off hardware interrupts. After completing the interaction, remember to turn on interrupts. In some series, this is called "entering/exiting the core." 3) Do not call any other non-reentrant functions. 4) Use the stack with caution. It is best to OS_ENTER_KERNAL before using it. Stack operations involve memory allocation. If you are not careful, it will cause overflow and overwrite the data of other tasks. Therefore, please use the stack with caution! It is best not to use it! Many hacker programs take advantage of this so that the system can execute illegal code and easily gain control of the system. There are some other rules. In short, always remember one sentence: make sure the interrupt is safe! Overloaded function Definition Overloaded function is a special case of function. For ease of use, C++ allows several functions with similar functions to be declared in the same scope, but the formal parameters (referring to the number, type or order of parameters) of these functions with the same name must be different, that is, the same operator is used to perform different calculation functions. This is called overloading a function. Overloading a function is often used to solve problems with similar functions but different data types. Two overloaded functions must differ in one or two of the following aspects: 1. The functions have different parameters. 2. The functions have different parameter types. This programming mechanism of C++ brings great convenience to programmers. There is no need to choose different function names for functions with similar functions but different parameters, which also enhances the readability of the program. The relevant regulations for C++ operator overloading are as follows: (1) The priority of the operator cannot be changed; (2) The associativity of the operator cannot be changed; (3) Default parameters cannot be used with overloaded operators; (4) The number of operands of an operator cannot be changed; (5) New operators cannot be created, only existing operators can be overloaded; (6) When an operator acts on a data type provided by C++, the original meaning remains unchanged. Overloaded function is a special function supported by C++. The judgment of function overloading by C++ compiler is one of the most complicated contents in C++ language. Definition of overloaded function: a special function with the same function name in the same declaration scope but different parameter lists, that is, a function that is uniquely identified and distinguished by the parameter list of the function. Why should functions be overloaded? When should function overloading be chosen? Function overloading is actually the idea of "one thing for multiple uses" (the "thing" here refers to the "function name"). In fact, not only functions can be overloaded, but operators can also be overloaded. For example, the operators "<<" and ">>" can be used as shift operators, insertion operators in output streams, and extraction operators in input streams. When you are going to define a group of functions that perform a series of operations, but they are applied to different parameter types. At this time, we can choose to overload the function. For example: int z_x_max (int, int); //Return the maximum value of two integers; int ve_max (const vector&); //Return the maximum value in the vector container; int matrix_max (const matrix &); //Return the maximum value of the matrix reference; The above three functions can be roughly described as determining the maximum value in a set of numbers. For the users of the functions, they do not care about the details of the function definition, that is, they do not care that different functions should be used to determine the size of two integers and the size of the number of arrays (vector containers). For the designers of the program, this is something they have to think about. The programmer must remember and look up each function name. Function overloading frees the programmer from the complexity of this problem, and C++ provides this support.