Using several articlesArticleA simple record of C and C ++ multi-thread programming BasicsProgramTo record.These chapters are organized from simple to deep,Dead dog!
Let's talk about it in the comments!
[CPP] View plaincopy
-
- # Include <iostream>
-
- # Include <pthread. h> // the header file is required and complies with POSIX standards to enable program porting to many platforms
-
-
- Using NamespaceSTD;
-
- # Define num_threads 5
-
-
- Void* Say_hello (Void* ARGs)// The Running function of the thread, which must be void *. If it is not specified, a universal pointer and a universal pointer are returned.
-
- {
-
- Cout <"Hello ..."<Endl;
-
- }
-
-
- IntMain ()
-
- {
- Pthread_t tids [num_threads];// Define the ID variable of the thread. Multiple variables can be declared as arrays.
-
- For(IntI = 0; I <num_threads; ++ I)
-
- {
-
- IntRet = pthread_create (& tids [I], null, say_hello, null );// The parameters are the ID of the created thread, the thread parameter, the name of the called function, and the input function parameters.
-
- If(Ret! = 0)
- {
-
- Cout <"Pthread_create error: error_code ="<RET <Endl;
-
- }
-
- }
-
-
- Pthread_exit (null );// Wait until the threads exit before the process ends. Otherwise, the process is forced to end and the thread may not respond;
-
- }
Compile command:
G ++-lpthread-O test. out test. cpp
Specifically, calling the static library file pthread is required, and then running the test, Owen!
[Plain] View plaincopy
- [CPP @ node2 pthread] $./ex_create
- Hello...
- Hello...
- Hello...
- Hello...
- Hello...
- [CPP @ node2 pthread] $