In the previous article, I gave a brief overview of the thread. the role it plays in the system and programming applications is self-evident. Learning it, mastering it, and thoroughly understanding it is a must for a programmer. In the following illustration, all thread operations are user-level operations. In Linux, The pthread thread library is generally a set of common thread libraries proposed by POSIX, so its portability is very good.
Creating a thread is actually determining the entry point for calling this thread function. Here, the usual function is pthread_create. After a thread is created, it starts to run related thread functions. At the end of the function, the thread also exits. This is one way to exit the thread. The other way to exit the thread is to call the pthread_exit () function interface, which is the active action of the function. Note that when using a thread function, do not call the exit () function easily, because this will cause the entire process to exit. A process usually contains multiple threads, therefore, after exit () is called, all threads in the process will be terminated. Therefore, pthread_exit is used in the thread to replace exit in the process.
Because data segments in a process are shared, the resources occupied by the exited thread are not released as the thread ends. Just as processes can call the wait () function to synchronize the middle finger and release resources, there is a similar mechanism between threads, that is, the pthread_join function. pthread_join can suspend the current thread and wait for the end of the thread. This function is a blocking function. The function that calls it will wait until the end of the waiting thread. When the function returns, the resources of the function to be waited for will be released.
1. Function syntax.
Pthread_create
Header file: pthread. h
Function prototype: int pthread_create (pthread_t * thread, pthread_attr_t * ATTR,
Void * (start_routine) (void *), void * Arg );
Function input value: thread ID
ATTR: thread attribute settings
Start_routine: thread function entry
Arg: The parameter passed to the thread function.
Return Value: 0: Successful
-1: Failed
Pthread_exit
Header file: pthread. h
Function prototype: void pthread_exit (void * retval );
Function input value: retval: the return value of the pthread_exit () caller's thread. Other functions such as pthread_join can be retrieved and obtained.
Phread_join
Header file: pthread. h
Function prototype: int pthread_join (pthread_t * thread, void ** thread_return );
Function input value: thread: The identifier of the waiting thread.
Thread_return: User-Defined pointer, used to store the return value of the waiting thread (not null );
Function return value: Success: 0
Failed:-1
2. Function example implementation.
# Include <stdlib. h> <br/> # include <stdio. h> <br/> # include <pthread. h> <br/> # include <errno. h> </P> <p> static void * pthread_func_1 (void *); <br/> static void * pthread_func_2 (void *); </P> <p> int main (INT argc, char ** argv) <br/> {<br/> pthread_t pt_1 = 0; <br/> pthread_t pt_2 = 0; <br/> int ret = 0; </P> <p> ret = pthread_create (& pt_1, null, pthread_func_1, null ); <br/> If (Ret! = 0) <br/>{< br/> perror ("pthread_1_create"); <br/>}</P> <p> ret = pthread_create (& pt_2, null, pthread_func_2, null); <br/> If (Ret! = 0) <br/>{< br/> perror ("pthread_2_create"); <br/>}</P> <p> pthread_join (pt_1, null ); <br/> pthread_join (pt_2, null); </P> <p> return 0; <br/>}</P> <p> static void * pthread_func_1 (void * Data) <br/>{< br/> int I = 0; </P> <p> for (; I <6; I ++) <br/> {<br/> printf ("this is pthread1! /N "); </P> <p> if (I = 2) <br/>{< br/> pthread_exit (0 ); <br/>}</P> <p> sleep (1); <br/>}</P> <p> return NULL; <br/>}</P> <p> static void * pthread_func_2 (void * Data) <br/>{< br/> int I = 0; </P> <p> for (; I <3; I ++) <br/> {<br/> printf ("this is pthread2! /N "); <br/>}</P> <p> pthread_exit (0); </P> <p> return NULL; <br/>}< br/>
In the preceding example, the thread is created, exited, and suspended. In the next article, we will introduce the attributes and settings of thread lines.
~~ End ~~