one, multi-threaded VS multi-process
Threads have many advantages over the process. Under the Linux system, starting a new process must be assigned to its own address space, creating numerous data tables to maintain code snippets and data. While running on multiple threads in a process, they use the same address space. It is thus possible to share data space between threads in the same process, data can be used with each other, and switching between threads is faster and can make more efficient use of the CPU.
Second, the program design
[Note] header file <pthread.h> compile time to load dynamic library libpthread.a, use-lpthread
1. Creating Threads
2. Waiting for Thread
3. Close the thread
4. Exit Clear
1. Creating Threads
int Pthread_create (pthread_t *tidp, const pthread_attr_t *attr, void * (*START_RTN) (void), void *arg)
TIDP is the thread ID, which is the value assigned by the function, so a pthread_t address is passed.
attr thread properties, usually empty.
Start_rtn is the function to be executed by the thread, the return value is a null pointer, and the argument is the following *arg
Returns 0 if successful, otherwise the error number is returned.
Cases:
#include <stdio.h>
#include <pthread.h></p> <p>void *func1 (void *arg) {//original function declaration
int i;
for (i=0;i<5;i++) {
printf ("This is func1! The num is%d\n ", * (int*) arg); Converts a null pointer to an int type pointer
Sleep (1);
}
}</p> <p>void *func2 (int *m) {//custom type declaration, or non-pointer type can be defined, but there is a warning when create, because the non-address does not change the value passed in
int i;
for (i=0;i<5;i++) {
printf ("This is func2! The num is%d\n ", *m);
(*m) + +;
Sleep (1);
}
}</p> <p>int Main () {
pthread_t Id1,id2;
int num = 5;
int *p = #
if (Pthread_create (&id1,null, (void *) func1, (void *) p)! = 0) {
printf ("Thread1 create error!\n");
return-1;
}
if (Pthread_create (&id2,null, (void *) func2,&num)! = 0) {
printf ("Thread2 create error!\n");
return-1;
}
Pthread_join (Id1,null); Wait for thread to end
Pthread_join (Id2,null);
printf ("Running complete!\n");
return 0;</p> <p>
}
Operation Result:
[Email protected] process]$ gcc thc.c-o thc-lpthread-g
[Email protected] process]$./thc
This is func2! The NUM is 5
This is func1! The NUM is 6
This is func2! The NUM is 6
This is func1! The NUM is 7
This is func2! The NUM is 7
This is func1! The NUM is 8
This is func2! The NUM is 8
This is func1! The NUM is 9
This is func2! The NUM is 9
This is func1! The NUM is 10
Running complete!
[[Email Protected]t process]$
2. Waiting for Thread
[note] When the Pthread_create function is called, the thread does not begin execution, and the main process should wait, such as with sleep, or with a more specialized function: Pthread_join
int Pthread_join (pthread_t tid, void **rval_ptr)
The calling function can block the calling thread until the specified thread terminates.
The TID is the return value of the function that waits for the exit thread to Id,rval_ptr. is a pointer to a pointer that can be placed empty.
Cases:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h></p> <p>void *func (int *p) {
int *num= (int *) malloc (sizeof (int)); Must be created dynamically for the reason that I can refer to my blog dynamically allocating memory
printf ("Please input the number:");
scanf ("%d", num);
return (void *) num; Type is specified by the Pthread_create parameter.
}</p> <p>int Main () {
pthread_t PTH;
void *a;
if (Pthread_create (&pth,null, (void *) func,null)! = 0) {
printf ("Create thread error!\n");
return 1;
}
Pthread_join (Pth,&a); Pointer to a null pointer
printf ("Get the num from the thread, it ' s%d\n", * (int *) a);
return 0;
}
3. Terminating threads
Thread termination has the following three ways:
1. The thread returns from the function
2. Thread can stop other functions
3. The thread calls the Pthread_exit function itself
void Pthread_exit (void *rval_ptr)
Rval_ptr a pointer to the return value of the thread exit, which is the function return value.
4. Exit Clear
void Pthread_cleanup_push (void (*RTN) (void*), void *arg)
RTN is a clear function, ARG is the parameter that clears the function
void Pthread_cleanup_pop (int execute)
The purge function is executed when execute is not 0 o'clock. is not executed at 0.
From the call point of the Pthread_cleanup_push to the program segment between Pthread_cleanup_pop, if there is an action to terminate the process, such as calling Pthread_exit or terminating abnormally (excluding return), the Pthread_ is executed. The cleanup function specified by Cleanup_push (). When multiple nested matches are matched, the nearest match.
Cases:
#include <stdio.h>
#include <pthread.h></p> <p>void *clean (char *argv) {
printf ("Clean was called by%s\n", argv);
return NULL;
}
void *func1 (void *argv) {
printf ("Welcome Enter the func1!\n");
Pthread_cleanup_push (void*) Clean, "The first time call!");
Pthread_cleanup_push (void*) Clean, "The second time call!");
if (argv) {
return (void *) 1; The second run bets off this sentence
}
Pthread_cleanup_pop (0);
Pthread_cleanup_pop (1);
return (void *) 0;
}</p> <p>void *func2 (void *argv) {
Sleep (1); Two threads run successively indeterminate
printf ("Welcome Enter the func2!\n");
Pthread_cleanup_push (void*) Clean, "The first time call!");
Pthread_cleanup_push (void*) Clean, "The second time call!");
if (argv) {
Pthread_exit (NULL);
}
Pthread_cleanup_pop (0);
Pthread_cleanup_pop (0);
return (void *) 0;
}</p> <p>
int main () {
pthread_t Tid1,tid2;
if (Pthread_create (&tid1,null, (void *) func1, (void *) 1)! = 0) {
printf ("Thread1 create error!\n");
return 1;
}</p> <p> if (Pthread_create (&tid2,null, (void *) FUNC2, (void *) 1)! = 0) {
printf ("Thread2 create error!\n");
return 1;
}
Pthread_join (Tid1,null);
Pthread_join (Tid2,null);
return 0;
}
Operation Result:
[Email protected] process]$ gcc thclean.c-o thclean-lpthread
[Email protected] process]$./thclean
Welcome Enter the func1!
Welcome Enter the func2!
Called by the second time call! First 2, 1.
Called by the first time call!
[Email protected] process]$ vim THCLEAN.C
[Email protected] process]$ gcc thclean.c-o thclean-lpthread
[Email protected] process]$./thclean
Welcome Enter the func1!
Called by the first time call! Second has been pop
Welcome Enter the func2!
Called by the second time call!
Called by the first time call!
[Email protected] process]$
Linux Multithreaded Programming Example