Reprinted: http://software.intel.com/zh-cn/blogs/2011/09/22/400007583/
 
1. Introduction:
 
Multithreading in Linux follows the POSIX thread interface, which is called pthread. To compile a multi-threaded program in Linux, you need to use the header file pthread. H. You need to use the library libpthread. A for connection. By the way, the implementation of pthread in Linux is achieved by calling clone. Clone () is a Linux-specific system call. It is used in a similar way as fork. For details about clone (), interested readers can refer to the relevant documentation.
 
Pthread_t is defined in the header file/usr/include/bits/pthreadtypes. h:
 
Typedef unsigned long int pthread_t;
 
It is the identifier of a thread.
 
2. Function Description:
 
(1) function prototype:
 
# Include
 
Int pthread_create (pthread_t * restrict TIDP, const pthread_attr_t * restrict ATTR, void * (* start_rtn) (void), void * restrict Arg );
 
Returned value: If a thread is successfully created, 0 is returned. Otherwise, the error number is returned.
 
Format parameters:
 
Pthread_t * restrict TIDP indicates the thread ID pointer of the thread to be created
 
Const pthread_attr_t * restrict ATTR specifies the thread attributes when a thread is created.
 
Void * (start_rtn) (void) returns a void pointer function.
 
Void * restrict Arg start_rtn row Parameter
 
Example 1:
 
Function: test the creation of a new thread.
 
Program name: pthread_test.c
 
# Include
 
# Include
 
Void * Create (void * Arg)
 
{
 
Printf ("New thread created .....");
 
}
 
Int main (INT argc, char * argv [])
 
{
 
Pthread_t TIDP;
 
Int error;
 
Error = pthread_create (& TIDP, null, create, null );
 
If (error! = 0)
 
{
 
Printf ("pthread_create is not created ...");
 
Return-1;
 
}
 
Printf ("prthread_create is created ...");
 
Return 0;
 
}
 
Compilation Method:
 
# Gcc-wall-lpthread pthread_test.c
 
Because the pthread library is not a Linux system library, you must add-lpthread during compilation. Otherwise, the compilation will fail and the following error will occur:
 
Thread_test.c: In the 'create' function:
 
Thread_test.c: 7: Warning: In a function with a returned value, the program flow reaches the end of the function.
 
/Tmp/ccobjmud. O: In function 'main': thread_test.c :(. Text + 0x4f): Undefined reference to 'pthread _ create'
 
Collect2: LD returns 1
 
2. Thread termination and Termination
 
(1) Terminate the thread
 
Function prototype: void pthread_exit (void * status );
 
Parameter: * retval indicates the exit status of the thread. This function is generally used when the thread exits.
 
Purpose: This function is used to exit the current thread and release the thread-related resources it uses.
 
Return Value: when the call thread is the last non-daemon thread in the process, the process exits with status 0. When the original thread returns from the main () function, the process exits with the return value of the main function of the thread.
 
(2) wait until the thread ends
 
The prototype is as follows:
 
Int pthread_join (pthread_t th, void ** thread_return)
 
Parameter: TH is the identifier of the waiting thread.
 
Thread_return is a user-defined pointer that can be used to store the return values of the waiting thread.
 
Function: The pthread_join function is used to wait for the end of a thread.
 
Note: This function is a thread-blocking function. The function called will wait until the end of the waiting thread. When the function returns, the resources of the waiting thread will be reclaimed. This function is used for synchronization between processes and threads to prevent other threads from being executed due to the early termination of the main thread. For example, if the main thread creates two threads, it is possible that when the two derived threads are not executed, the main thread has ended, and the main thread has ended. For the system, this means that the process has ended, so the derived thread naturally has no chance to execute it. Therefore, pthread_join is used to wait for the end of the derived thread.
 
Return Value: if the call is successful, 0 is returned. Otherwise, a non-0 error code is returned.
 
(3) Separation thread pthread_detach
 
Int pthread_detach (pthread_t tid );
 
Purpose: Set a non-detached thread to a detached thread. That is, the notification thread library recycles the memory and other resources occupied by the thread when the specified thread is terminated.
 
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
 
The results of using pthread_detach multiple times on one thread are unpredictable.
 
(4) thread Cancellation
 
When we want to terminate a thread, we can use the pthread_cancel () function (). Its prototype is defined as follows:
 
Prototype: int pthread_cancel (pthread_t thread );
 
Description: This function uses a thread ID as a parameter to send a cancellation request to this thread. The way the thread processes this request depends on the state of the thread. It may reflect it immediately, or wait until it gets a cancellation point to take action, or ignore it completely.
 
(5) set the thread cancellation status
 
The thread can use the pthread_setcancelstate function to set its cancellation status,
 
The prototype is defined as follows: int pthread_setcancelstate (INT state, int * oldstate );
 
Parameter description:
 
The value of state can be pthread_cnacel_enable, which allows the thread to receive cancellation requests or pthread_cnacel_enable, which is used to ignore cancellation requests.
 
Oldstate is used to obtain the previous cancellation status. If the cancellation request is accepted, the thread can enter the second control level and use pthread_setcanceltype to set the cancellation type.
 
(6) set the cancellation type
 
The prototype is defined as follows: int pthread_setcanceltype (INT type, int * oldtype );
 
Parameter description:
 
The value of type can be pthread_cancel_asynchronous, which causes the thread to process immediately after receiving the cancellation request (asynchronous) or pthread_cancel_deferred, which will delay processing after receiving the cancellation request, it is not processed until a defer is encountered.
 
Odtype allows you to save the previous state. If you do not want to wait until the previous state, you can pass null to it.
 
By default, the canceling status of a thread during startup is pthread_cnacel_enable, And the canceling type is pthread_cancel_deferred.