If you are using Linux/Unix/MacOSX, we can start. If you are using Windows, you need to download the pthread windows development kit from the website, fortunately, he is very small. The website address is http://sourceware.org/pthreads-win32/
Let's take a look at a basic example: program code # include <pthread. h>
# Include <iostream>
Using namespace STD;
Void * tprocess1 (void * ARGs ){
While (1 ){
Cout <"tprocess1" <Endl;
}
Return NULL;
}
Void * tprocess2 (void * ARGs ){
While (1 ){
Cout <"tprocess2" <Endl;
}
Return NULL;
}
Int main (){
Pthread_t T1;
Pthread_t T2;
Pthread_create (& T1, null, tprocess1, null );
Pthread_create (& T2, null, tprocess2, null );
Pthread_join (T1, null );
Return 0;
}
In the above example, we first added pthread. h file inclusion, which is required by the pthread multi-threaded program. Next, iostream is used for input and output, followed by the definition of two functions, which is no different from that of common functions, the program code void * tprocess1 (void * ARGs)
This form is completely designed to cater to the parameter type of the pthread_create function. You can also define it as long as you forcibly convert the pointer type when calling the pthread_create thread.
These two functions will be used as the thread execution bodies, that is, they will be run simultaneously in two threads.
Now let's take a look at the main function. All the calls related to pthread are here.
Pthread_t is a thread structure used to store thread-related data. You can also think of it as a thread type and declare a thread object (variable ). Program code pthread_t T1;
Pthread_t T2;
Here we declare two thread variables t1 and t2
Program code pthread_create (& T1, null, tprocess1, null );
Pthread_create (& T2, null, tprocess2, null );
These two statements are very important. pthread_create is used to create and start a thread. Its prototype is the program code int pthread_create (pthread_t * thread, pthread_attr_t * ATTR, void * (* start_routine) (void *), void * Arg );
We can know that the first parameter is the thread pointer, the second parameter is the thread attribute pointer, And the thread attribute pthread_attr_t is used to specify attributes such as thread priority. In general, we do not need to modify the parameter, the default attribute is used to construct a thread. Therefore, null is usually used here. The third parameter is a function pointer (function pointer? What have you heard ?...... Dizzy, well, you should review the C or C ++ pointer) is the code to be executed by the thread. Here we will execute tprocess1 tprocess2 and write it as above, here, the type definition of this function pointer is to return a null type pointer and receive a function pointer of the null type pointer parameter. If your function is not defined in this way, then you can convert it directly.
After writing these two lines of code, the two threads have been executed, but if you omit the program code pthread_join (T1, null );
Then when you try to compile and run the program, you will find that the program exits when nothing appears to be done. Yes, this is because when the main thread of the program exits, the operating system will close all resources used by the application, including threads ...... So before the main function ends, we have to find a way to stop the program. The function of the pthread_join method is to wait for the thread to end. The thread to wait is the first parameter, and the program will stop at this place, until the thread ends, the second parameter is used to accept the return value of the thread function. It is a void ** type pointer. If there is no return value, set it to null directly.
After the program is written, how can we compile and run it?
If you are using Linux:
Enter
G ++ thread. cpp-othread-lpthread
./Thread
Compile and run the program.
If you are using VC:
Add several library files in the development package to the Project Properties
Put the DLL files in your project path, that is, the working path when the program runs. This seems to be different in vc6 and 2005. If you are not sure, put it directly in system32...
The following work is very simple.
Click "run" and prompt for compilation. OK. The result is displayed...
Isn't multithreading so simple? I think you can write a simple multi-threaded program after just a few lines of code. Haha, the problem is actually not that simple, we need to deal with Thread Synchronization in multiple threads. I will introduce it to you in the next topic.