Finishing thread

One,

1, thread attributes pthread_attr_t Posix threads include the scope attribute, detach property, stack address, stack size, priority. (In the pthread_create, the second parameter is set to NULL, it will use the default properties).

2, thread attributes pthread_attr_t.

( 1) defined in pthread_attr_t /usr/include/bits/pthreadtypes.h file is:

typedef union

{

  char __size[__SIZEOF_PTHREAD_ATTR_T];

  long int __align;

} pthread_attr_t;

  Since pthread do not want users to see the space inside it to achieve pthread_attr_t because API designed to encapsulate this type. So long as it provides pthread_attr_t size and internal implementation of the same type on it.

( 2) which is the same definition content is substantially uniform, and the size of the structure are equal.

typedef struct

{

   int __detachstate; separated state of a thread

   int __schedpolicy; thread scheduling policy

 struct sched_param __schedparam; thread scheduling parameters

    int __inheritsched; inheritance thread

   Int __scope; scoped thread

   size_t    __guardsize;   

   int    __stackaddr_set;

The position of the thread stacks; void * __stackaddr

   size_t __stacksize; thread stack size

}pthread_attr_t;

3, meaning the main attributes of pthread_attr_t as follows:

( 1) __ the detachstate, indicating whether the new threads in the process out of sync with the other thread, if set to PTHREAD_CREATE_DETACHED the new thread can not be used pthread_join () to synchronize and release the resources occupied by itself when you exit. PTHREAD_CREATE_JOINABLE default state. This property can also create and run later in the thread set with pthread_detach (), and once set PTHREAD_CREATE_DETACH state (either set or set to create run-time) can no longer be restored to PTHREAD_CREATE_JOINABLE state.

( 2) __ schedpolicy, represents a new thread scheduling policy, including SCHED_OTHER (normal, non-real-time), SCHED_RR (real-time, round-robin) and SCHED_FIFO (realtime, first-in first-out) are three default SCHED_OTHER, after two scheduling only super user policy effective. It can be used pthread_setschedparam runtime () to change.

( 3) __ schedparam, a struct sched_param structure, there are only a sched_priority integer variable represents the run priority thread. This parameter only when the real-time scheduling policy is only (ie SCHED_RR or SCHED_FIFO) effective, and can be changed by pthread_setschedparam () function at run-time, the default is 0.

( 4) __ inheritsched, there are two values to choose from: PTHREAD_EXPLICIT_SCHED and PTHREAD_INHERIT_SCHED, the former said that the new thread uses explicitly specified scheduling policy and scheduling parameters (ie, the value of attr), which represents the value of inherited caller thread. The default is PTHREAD_EXPLICIT_SCHED.

( 5) __ scope, indicate the scope for competition between the threads of the CPU, which means that the effective range of thread priority. The POSIX standard defines two values: PTHREAD_SCOPE_SYSTEM and PTHREAD_SCOPE_PROCESS, the former said to compete with the system CPU time for all threads, which represents the only thread compete with the process and the CPU. LinuxThreads currently only implements a PTHREAD_SCOPE_SYSTEM value.

supplement:

  To set these properties, the POSIX function defines a set of properties settings, including pthread_attr_init (), pthread_attr_destroy () and attributes associated with each pthread_attr_getXXX / pthread_attr_setXXX function.

Set thread attributes  before pthread_attr_t, usually pthread_attr_init first call to initialize, it then calls the appropriate property settings function.

two,

1、pthread_attr_init

Function: initialize the thread attributes variable.

Header: <pthread.h>

Function Prototype: int pthread_attr_init (the pthread_attr_t is * attr);

Incoming function value: attr: thread attributes.

Function Return Value: Success: 0; failed: -1.

 

2、pthread_attr_setscope

Function: Set the thread __scope property.

( Scope CPU attribute represents the range of the competition between the threads, i.e. .POSIX thread priority criteria defined effective range of two values: PTHREAD_SCOPE_SYSTEM and a PTHREAD_SCOPE_PROCESS, the former represents the competition with the system CPU time for all threads, the latter represents only with the same threads in the process competitive CPU. the default is PTHREAD_SCOPE_PROCESS. LinuxThreads currently only implements a PTHREAD_SCOPE_SYSTEM value).

Header: <pthread.h>

Function Prototype: int pthread_attr_setscope (the pthread_attr_t is * attr, int scope);

Incoming function value: attr: thread attributes.

scope: PTHREAD_SCOPE_SYSTEM, represent competing for CPU time with all threads in the system;

    PTHREAD_SCOPE_PROCESS, represents only compete with the threads in the process CPU.

Function Return Value: Success: 0; failed: -1.

 

3、pthread_attr_setdetachstate

Function: Set the thread detachstate property.

(Which indicates whether the new thread and other threads in the process out of sync, if set to PTHREAD_CREATE_DETACHED the new thread can not be used pthread_join () to synchronize and release the resources occupied by itself when you exit. PTHREAD_CREATE_JOINABLE default state. This property can also be after the thread used to create and run pthread_detach () to set up, and once set PTHREAD_CREATE_DETACH state (either set or set to create run-time) can no longer be restored to PTHREAD_CREATE_JOINABLE state).

Header: <phread.h>

函数原型:int pthread_attr_setdetachstate(pthread_attr_t* attr,int detachstate);

Incoming function value: attr: thread attributes.

detachstate: PTHREAD_CREATE_DETACHED, can not be used pthread_join () to synchronize and release the resources occupied by itself when you exit;

          PTHREAD_CREATE_JOINABLE, can pthread_join () to synchronize.

Function Return Value: Success: 0; failed: -1.

 

4、pthread_attr_setschedparam

Function: Set the thread schedparam property, namely priority call.

Header: <pthread.h>

Function Prototype: int The pthread_attr_setschedparam (* attr the pthread_attr_t is, the sched_param * struct param);

Incoming function value: attr: thread attributes.

param: thread priority.

(A struct sched_param structure, there are only a sched_priority integer variable indicates the operating priority of the thread. This parameter only when the real-time scheduling policy (ie SCHED_RR or SCHED_FIFO) is only effective, and can pthread_setschedparam at runtime () function change default is 0).

Function Return Value: Success: 0; failed: -1.

 

5、pthread_attr_getschedparam

Function: Get the thread priority.

Header: <pthread.h>

Function Prototype: int pthread_attr_getschedparam (* attr the pthread_attr_t is, the sched_param * struct param);

Incoming function value: attr: thread attribute;

           param: thread priority;

Function Return Value: Success: 0; failed: -1.

 

 

Third, the thread ID:

pthread_t pthread_self(void);

// pthread_t data type; obtained to pthread_self function thread ID,

int pthread_equal(pthread_t tid1,pthread_t tid2);

// determines whether the ID is equal to two.

 

Fourth, create a thread

int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);

// The first parameter is tidp pthread_t (point thread identifier ) of the pointer , when the function returns, ID tidp point core assigned to a thread.

// The second parameter is used to set the thread arrt property, if it is not NULL it indicates specifying its properties.

// The third parameter start_rtn is (address of the thread running function) function pointer. Which points to the function's return value is a void pointer type, parameters are. start_rtn function points are the subject of the thread that created out of pthread_t function thread from start_rtn start address of the function pointed to begin, when the function returns the thread to stop running.

// last parameter arg is a void pointer type (a function of operating parameters). arg is start_rtn parameters of the function pointed to, when the thread begins execution, the parameters passed to the kernel thread is responsible. If successfully created a thread, pthread_create function returns 0.

 

Fives,

1, initialization attribute thread attributes. Such as:

pthread_attr_t t_Attr; // attr: Thread Attributes

pthread_attr_init (& t_Attr); // attr incoming value, the return value success is 0 , failed to  -1

pthread_attr_setdetachstate(&t_Attr,PTHREAD_CREATE_DETACHED);

// new thread can not be used pthread_join () to synchronize and release the resources occupied by itself when you exit.

 

2、Sleep(m);

Sleep m seconds to ensure the newly created thread is called before the main thread.

 

Guess you like

Origin www.cnblogs.com/L-102/p/11442061.html