This is a column about POSIX threading programming. The author, on the basis of clarifying concepts, will give you a detailed account of the POSIX line threading API. This is the first article that will tell you about the creation and cancellation of threads.
Unlike a fork () call to create a process, the thread created by Pthread_create () does not have the same sequence of execution as the thread that invokes the Pthread_create (), but rather makes it run the Start_routine (ARG) function. Thread returns the threads ID created, and attr is the thread attribute set when the thread is created (see below). The return value of Pthread_create () indicates whether the thread creation was successful. Although ARG is a variable of type void, it can also be passed to the Start_routine () function as an argument of any type, while Start_routine () can return a return value of a void * type, which can be other types, and is Pthread_join () obtained.
1. 3 Thread Creation Properties
The attr parameter in Pthread_create () is a structure pointer, and the elements in the structure correspond to the running properties of the new thread, including the following:
__detachstate, which indicates whether the new thread is out of sync with other threads in the process, if the new thread cannot synchronize with the Pthread_join (), and releases the resource that it occupies when exiting. The default is the Pthread_create_joinable state. This property can also be set with Pthread_detach () after the thread is created and run, and once set to the Pthread_create_detach state (whether set at creation or runtime), it cannot be restored to Pthread_create_ Joinable state.
__schedpolicy, which represents the scheduling strategy for new threads, mainly includes Sched_other (normal, Non-real time), SCHED_RR (real-time, rotary) and Sched_fifo (real time, first in first out) three kinds, the default is Sched_other, The last two scheduling policies are valid only for Superuser. The runtime can be changed using Pthread_setschedparam ().
__schedparam, a struct SCHED_PARAM structure, currently has only one sched_priority integer variable that represents the running priority of the thread. This parameter is valid only if the scheduling policy is real-time (i.e. SCHED_RR or SCHED_FIFO) and can be changed at run time by the Pthread_setschedparam () function, which defaults to 0.
__inheritsched, there are two values to choose from: Pthread_explicit_sched and pthread_inherit_sched, which means that the new thread uses an explicit scheduling policy and a schedule parameter (that is, a value in attr). The latter represents the value of the inherited caller thread. The default is pthread_explicit_sched.
__scope, which represents the range of competing CPUs between threads, that is, the valid range of thread precedence. The POSIX standard defines two values: Pthread_scope_system and pthread_scope_process, which represent competing CPU time with all threads in the system, which means that only the CPU is competing with threads in the process. At present, the Linuxthreads only implements a Pthread_scope_system value.
There are some values in the pthread_attr_t structure, but they are not set using Pthread_create ().
To set these properties, POSIX defines a series of property-setting functions, including Pthread_attr_init (), Pthread_attr_destroy (), and pthread_attr_get that are associated with each property/pthread_ Attr_set---function.
1. 4 thread-created Linux implementations
We know that the thread implementation of Linux is done outside of the core, providing the interface for creating the process do_fork (). The kernel provides two system calls __clone () and fork (), which eventually invoke the Do_fork () kernel API with different parameters. Of course, in order to implement threads, there is no core for multiple processes (in fact, lightweight processes) to share data segment support is not, therefore, do_fork () provides a number of parameters, including CLONE_VM (shared memory space), CLONE_FS (shared file system information), Clone_ Files (shared file descriptor tables), Clone_sighand (Shared signal handle table), and clone_pid (shared process ID, valid only for the nuclear process, that is, the No. 0 process). When using the fork system call, the kernel calls Do_fork () does not use any shared properties, the process has a separate running environment, and when the thread is created using Pthread_create (), all of these properties are eventually set to invoke __clone (). These parameters are all passed to Do_fork () in the kernel, thus creating a "process" that has a shared running environment in which only the stack is self-contained and passed by __clone ().
Linux threads exist in the kernel as lightweight processes, have separate process table entries, and all creation, synchronization, and deletion operations are performed in the Pthread library. The Pthread library uses an administrative thread (__pthread_manager (), each process is independent and unique) to manage the creation and termination of threads, assign thread IDs to threads, send thread-related signals (such as cancel), and the main thread (Pthread_create () The caller passes the request information to the management thread through a pipe.
2. 4 Program Design Considerations
If the thread is in an infinite loop and the loop body does not have the necessary path to the cancellation point, the thread cannot be terminated by the cancellation request of the outside other thread. Therefore, the Pthread_testcancel () call should be added to the necessary path of such a loop body.
2. The 5 pthread function
Int pthread_cancel (pthread_t thread)
, which is associated with the thread cancellation, sends an abort signal to the thread thread and returns 0 if successful, otherwise it is not a 0 value. Sending success does not mean that thread will terminate.
Int pthread_setcancelstate (int state, int *oldstate)
Sets the response of this thread to the cancel signal. State has two values: pthread_cancel_enable (default) and pthread_cancel_disable, which respectively indicate that the signal is cancled and the CANCEL signal is continued; old_ State, if not null, is deposited in the original cancel status for recovery.
Int pthread_setcanceltype (int type, int *oldtype)
Sets the time that this thread cancels the action. Type is evaluated by two values: Pthread_cancel_deffered and pthread_cancel_asychronous, only if the CANCEL state is enabled. Each indicates that the signal continues to run to the next cancellation point and then exits and immediately performs the Cancel action (exit), and oldtype the canceled action type value if it is not null.
Void pthread_testcancel (void)
checks whether this thread is in the Canceld state and, if so, cancels the action, or returns directly.