Initialize/Destroy Thread properties
int Pthread_attr_init (pthread_attr_t *attr); int Pthread_attr_destroy (pthread_attr_t *attr);
Thread Detach Properties
int pthread_attr_getdetachstate (const pthread_attr_t *attr, int *detachstate); int Pthread_attr_setdetachstate ( pthread_attr_t *attr, int detachstate);
Parameter description:
The following values may is specified in Detachstate:
Pthread_create_detached
Threads that is created using attr would be is created in a detached state.
Pthread_create_joinable
Threads that is created using attr would be is created in a joinable state.
The default setting of the Detach state attribute in a newly initialized
Threadattributes object is pthread_create_joinable.
The Pthread_attr_getdetachstate () returns the detach state attribute of the
Threadattributes object attr in the buffer pointed to by Detachstate.
Thread stack size
int Pthread_attr_setstacksize (pthread_attr_t *attr, size_t stacksize); int pthread_attr_getstacksize (pthread_attr_t * attr, size_t *stacksize);
DESCRIPTION
The Pthread_attr_setstacksize () function sets the stack size attribute
Of the Threadattributes object referred to by attr to the value specified
In stacksize. (In general, this value is set to 0, using the system default setting of the thread stack size, otherwise it may cause problems with the portability of the program ) The stack size attribute determines the minimum size (in bytes) that would
Be allocatedfor threads created using the thread attributes object attr.
The Pthread_attr_getstacksize () function returns the stack size attribute of
The Threadattributes object referred to is attr in the buffer pointed through stacksize.
Thread stacks overflow protected area size
int Pthread_attr_setguardsize (pthread_attr_t *attr, size_t guardsize); int pthread_attr_getguardsize (pthread_attr_t * attr, size_t *guardsize);
Thread competition Range (process-wide competition or system-wide competition)
int Pthread_attr_getscope (const pthread_attr_t *attr,int *contentionscope); int Pthread_attr_setscope (pthread_attr_t *attr, int contentionscope);
Contentionscope Description:
Pthread_scope_system
The thread competes for resources with all and threads in all processes on
The system is in the same scheduling allocation domain (a group of one or more processors).
Pthread_scope_system threads is scheduled relative to one anotheraccording to their
scheduling policy and priority.
Pthread_scope_process
The thread competes for resources and all other threads in the same process Thatwere
Also created with the pthread_scope_process contention SCOPE.
Pthread_scope_process threads is scheduled relative to other threads in the PROCESS
According to their scheduling policy and priority. Posix.1-2001 leaves it unspecified how these threads contend with other threads in other process
On the system or with other threads in the same process this were created with the
Pthread_scope_system contention SCOPE.
Thread Scheduling Policy
int Pthread_attr_getschedpolicy (const pthread_attr_t *attr, int *policy); int Pthread_attr_setschedpolicy (Pthread_ attr_t *attr, int policy);
DESCRIPTION
The Pthread_attr_setschedpolicy () function sets the scheduling policy attribute of the thread
Attributes object referred to by attr to the value of specified in policy. This attribute determines
The scheduling policy of a thread created using the thread attributes object attr.
The supported values for policy is Sched_fifo, SCHED_RR, and Sched_other, as below:
Sched_fifo A first-in, first-out policy (advanced first-out scheduling strategy);
SCHED_RR a round-robin policy (time slice rotation scheduling algorithm);
Sched_other The standard Round-robin time-sharing policy (once the thread starts running until it is preempted or until the thread is blocked or stopped);
Attention:
In order for the policy setting made by Pthread_attr_setschedpolicy ()
To has effect when calling Pthread_create (3), the caller must use Pthread_attr_setinheritsched (3) (see below)
To set the Inherit-scheduler attribute of the Attributes object attr to pthread_explicit_sched (the newly created process inherits its own schedule property).
Scheduling Policy for thread inheritance
int pthread_attr_getinheritsched (const pthread_attr_t *attr, int *inheritsched); int pthread_attr_setinheritsched ( pthread_attr_t *attr, int inheritsched);
The following values may is specified in inheritsched:
Pthread_inherit_sched (Inheritance Schedule property)
Threads that is created using attr inherit scheduling attributes from the
creating thread; The scheduling attributes in attr is ignored.
Pthread_explicit_sched (Specify its own schedule property)
Threads that is created using attr take their scheduling attributes from
The values specified by the attributes object.
The default setting of the Inherit-scheduler attribute in a newly initialized thread attributes object is Pthread_inh Erit_sched.
Thread scheduling parameters (in fact we generally only care about one parameter: Thread priority, default is 0)
int Pthread_attr_getschedparam (const pthread_attr_t *attr, struct sched_param *param); int Pthread_attr_setschedparam ( pthread_attr_t *attr, const struct Sched_param *param);
Sched_param structure struct Sched_param { int sched_priority; /* Scheduling priority */};
concurrency level for threads
int pthread_setconcurrency (int new_level); int pthread_getconcurrency (void);
Description: The concurrency level is only valid in the N:M threading model, setting the concurrency level, giving the kernel a hint: the core thread that provides a given number of levels to map user threads is efficient (just a hint), default is 0, and the kernel is concurrency by default;
/** View Thread Default properties **/void printthreadattr () {pthread_attr_t attr; Pthread_attr_init (&ATTR); int detachstate; Pthread_attr_getdetachstate (&attr, &detachstate); cout << "detach-state:" << (detachstate = = pthread_create_joinable?) "Pthread_create_joinable": "pthread_create_detached") << Endl; size_t size; Pthread_attr_getstacksize (&attr, &size); cout << "stack-size:" << size << Endl; Pthread_attr_getguardsize (&attr, &size); cout << "guard-size:" << size << Endl; int scope; Pthread_attr_getscope (&attr, &scope); cout << "Scope:" << (scope = = Pthread_scope_system?) "Pthread_scope_system": "Pthread_scope_process") << Endl; int policy; Pthread_attr_getschedpolicy (&attr, &policy); cout << "policy:"; Switch (policy) {case Sched_fifo:cout << sched_FIFO "; Break Case Sched_rr:cout << "SCHED_RR"; Break Case Sched_other:cout << "Sched_other"; Break Default:break; } cout << Endl; int inheritsched; Pthread_attr_getinheritsched (&attr, &inheritsched); cout << "inheritsched:" << (inheritsched = = pthread_inherit_sched?) "pthread_inherit_sched": "pthread_inherit_sched") << Endl; struct Sched_param param; Pthread_attr_getschedparam (&attr,? m); cout << "Scheduling priority:" << param.sched_priority << Endl; cout << "Concurrency:" << pthread_getconcurrency () << Endl; Pthread_attr_destroy (&attr);}
Description
Binding properties:
Linux uses a "one-on-one" threading mechanism, which is a user thread that corresponds to a kernel thread . A binding attribute means that a user thread is pinned to a kernel thread, because the CPU time slice is scheduled to target a kernel thread (that is, a lightweight process) , so a thread with a bound attribute can guarantee that there will always be a kernel thread corresponding to it when needed. The corresponding non-binding attribute means that the relationship between the user thread and the kernel thread is not always fixed, but is controlled by the system.
Detach attribute:
The detach attribute is used to determine how a thread terminates itself. In a non-detached situation, when a thread ends, the system resources it consumes are not released, that is, there is no real termination. only when the Pthread_join () function returns does the created thread release the system resources that it occupies . In the case of detached attributes, the system resources that it occupies are released immediately at the end of a thread. one thing to note here is that if you set the Detach property of a thread, and the thread runs very fast, it is likely to terminate before the Pthread_create () function returns, and it may pass the thread number and system resources to other threads for use after it terminates .
Linux multithreaded Practice (3)--Thread properties