Thread
The thread is the smallest unit running independently in the computer, the runtime consumes very little system resources, because each thread consumes the CPU time is allocated by the system, therefore can regard the thread as the system allocates the CPU time the basic unit, in the user's view, the multi-thread is alternately executes, the system constantly switches between the various threads, Each thread can gain control of the CPU only within the time slice that the system allocates to him, executing the code in the thread
Advantages of threading: Save, conserve resources, save time, improve application responsiveness, improve multiprocessor efficiency, and improve program structure
Threads also have a lot of private data: Thread number, register, stack, signal mask, priority, thread-private storage space
A Create Thread Pthread_create function
#include <pthread.h>
int pthread_create (pthread_t *thread,const pthread_attr_t *attr, (void*) (*start_routine) (void*), void *arg);
The first parameter is a pointer to the thread identifier that is used to return the ID of the created thread when the thread is created successfully.
The second parameter is used to set the thread property, and Null to represent the default property.
The third parameter is the starting address of the thread's running function, which is a function pointer to the function to be called after the thread is created, which is called by the thread function.
The last parameter points to the argument passed to the thread function.
Upon successful creation, the function returns 0, and the memory unit pointed to by the thread is set to the thread ID of the newly created thread. The attr parameter is used to specify a variety of different thread properties. The newly created thread runs from the address of the Start_routine function, which has only one universal pointer parameter, ARG, and if more than one parameter needs to be passed to the Start_routine function, then these parameters need to be placed in a struct. The address of this structure is then passed in as a parameter to ARG.
Linux under the C language development of multi-threaded programs, Linux system multithreading following the POSIX thread interface, called Pthread.
A pointer modified by restrict is the only way to access the object that the pointer points to, but only if the second pointer is based on the first one. Access to objects is restricted to pointer expressions that are based on restrict adornments. Pointers decorated by restrict are used primarily for function parameters, or to point to memory space allocated by malloc (). The Restrict data type does not change the semantics of the program. The compiler is able to better optimize some types of routines by making restrict-modified pointers the only way to access the object's assumptions.
Two Thread Properties
Create a function pthread_create has a parameter of type pthread_attr_t, the struct is defined as follows
typedef struct
{
int detachstate; Separation State of threads
int schedpolicy; Thread Scheduling Policy
struct Sched_param schedparam; Scheduling Parameters for Threads
int inheritsched; The inheritance of threads
int scope; Scope of the thread
size_t Guardsize; Alert buffer size At the end of the thread stack
int stackaddr_set; Address set for Stack heap
void * STACKADDR; The location of the line stacks
size_t stacksize; The size of the line stacks
}pthread_attr_t;
. Detachstate, which indicates whether the new thread is out of sync with other threads in the process, and if set to pthread_create_detached, the new thread cannot synchronize with Pthread_join () and frees the resources it consumes at exit. The default is 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 status.
. Schedpolicy, represents the new thread scheduling strategy, mainly including sched_other (normal, non-real-time), SCHED_RR (real-time, rotation method) and Sched_fifo (real-time, first-in, first-out) three, the default is Sched_other, The latter two scheduling policies are only valid for super users. The runtime can be changed using over Pthread_setschedparam ().
. Schedparam, a struct SCHED_PARAM structure that currently has only one sched_priority integer variable representing the thread's run priority. This parameter is valid only if the scheduling policy is real-time (that is, SCHED_RR or SCHED_FIFO) and can be changed at run time through the Pthread_setschedparam () function, which defaults to 0.
. Inheritsched, there are two values to choose from: Pthread_explicit_sched and pthread_inherit_sched, which indicates that the new thread uses the explicitly specified schedule policy and schedule parameters (that is, values in attr). The latter represents the value that inherits the 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 priorities. 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 indicates that only the threads in the same process compete with the CPU. Currently, Linuxthreads only implements the Pthread_scope_system value.
Linux C Note Line program control (i)