Thread attributes pthread_attr_t Profile (reprint)

Original Address: https: //blog.csdn.net/hudashi/article/details/7709413 
Properties pthread_attr_t Posix threads in the thread attributes include scope, detach property, stack address, stack size, priority. In pthread_create, the second parameter set to NULL, it will use the default properties.
The main significance of the property pthread_attr_t as follows:
__detachstate, indicating 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 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.
__schedpolicy, it 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 only two scheduling strategies superuser effective. It can be used pthread_setschedparam runtime () to change.
__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.
__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.
__scope, it indicates the scope for competition between the threads of the CPU, which means 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.
  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.
Before setting the thread attributes pthread_attr_t, usually pthread_attr_init first call to initialize, it then calls the appropriate property settings function.
The main function is as follows:
1、pthread_attr_init
Function: initialize the thread attributes variable.
Header: <pthread.h>
Function Prototype: int pthread_attr_init (pthread_attr_t * attr);
Incoming function value: attr: thread attributes.
Function return values: Success: 0
                Failure: -1
2、pthread_attr_setscope
Function: Set the thread __scope property. scope attribute indicates 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. The default is PTHREAD_SCOPE_PROCESS. LinuxThreads currently only implements a PTHREAD_SCOPE_SYSTEM value.
Header: <pthread.h>
Function Prototype: int pthread_attr_setscope (pthread_attr_t * 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 the only thread to compete with the process of the CPU
The function returns worth: with 1.
3、pthread_attr_setdetachstate
Function: Set the thread detachstate property. This indicates whether 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 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.
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
The function returns worth: with 1.
4、pthread_attr_setschedparam
Function: Set the thread schedparam property, namely priority call.
Header: <pthread.h>
Function Prototype: int pthread_attr_setschedparam (pthread_attr_t * attr, struct sched_param * param);
Incoming function value: attr: thread attributes.
                 param: thread priority. A struct sched_param structure, there are only a sched_priority integer variable represents the run priority thread. This parameter is only valid when the real-time scheduling policy (ie SCHED_RR or SCHED_FIFO), and can be changed by pthread_setschedparam () function at run-time, the default is 0
Function Return: with 1.
5、pthread_attr_getschedparam
Function: Get the thread priority.
Header: <pthread.h>
Function Prototype: int pthread_attr_getschedparam (pthread_attr_t * attr, struct sched_param * param);
Incoming function value: attr: thread attribute;
                    param: thread priority;
Function Return: with 1.
Example 1:
 
 

 

#include <stdlib.h>   
#include <stdio.h>   
#include <errno.h>   
#include <pthread.h>   
 
static void pthread_func_1 (void);   
static void pthread_func_2 (void);   
  
int main (int argc, char** argv)   
{   
  pthread_t pt_1 = 0;   
  pthread_t pt_2 = 0;   
  pthread_attr_t atrr = {0};   
  an int  Ret  = 0 ;    
  
/ * Initialize attribute thread attributes * /
  pthread_attr_init (&attr);   
  pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);   
  pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);   
     
  ret = pthread_create (&pt_1, &attr, pthread_func_1, NULL);   
  if (ret != 0)   
  {   
    perror ("pthread_1_create");   
  }   
     
  ret = pthread_create (&pt_2, NULL, pthread_func_2, NULL);   
  if (ret != 0)   
  {   
    perror ("pthread_2_create");   
  }   
  
  pthread_join (pt_2, NULL);   
  
  return 0;   
}   
  
static void pthread_func_1 (void)   
{   
  int i = 0;   
     
  for (; i < 6; i++)   
  {    
    printf ("This is pthread_1.\n");   
      
    if (== 2)   
    {   
      pthread_exit (0);   
    }   
  }   
  
  return;   
}   
  
static void pthread_func_2 (void)   
{   
  int i = 0;   
  
  for (; i < 3; i ++)   
  {   
    printf ("This is pthread_2.\n");   
  }   
  
  return;   
}  
From the above examples, you can get such a result, the thread is a thread on the end of a function to automatically release resources, we have to wait until the two threads pthread_join to free system resources.

 

Guess you like

Origin www.cnblogs.com/webcyh/p/11316747.html