POSIX multithreaded Programming-Threading Properties __ Programming

Source: Internet
Author: User
Tags function prototype inheritance mutex posix

A Thread Properties
A thread has an attribute, which, in pthread_attr_t, must be initialized before it is processed, and it needs to be initialized after use. We initialize it with the Pthread_attr_init function and initialize it with Pthread_attr_destroy.
1. Name:Pthread_attr_init/pthread_attr_destroy
Function: Initialize/Remove initialization of thread properties
Header files: #include <pthread.h>
Function prototype: int pthread_attr_init (pthread_attr_t *attr);
int Pthread_attr_destroy (pthread_attr_t *attr);
Parameters: Attr Thread Property variables
Return value: If 0 is returned successfully, if failure returns-1.
After calling Pthread_attr_init, the pthread_t structure contains the default values for all properties of the thread supported by the operating system implementation.
If you want to remove initialization of the pthread_attr_t structure, you can call the Pthread_attr_destroy function.If the Property object is allocated dynamic memory space when the Pthread_attr_init is implemented, Pthread_attr_destroy also initializes the Property object with an invalid value, so if the pthread_attr_t structure that is initialized after Pthread_attr_destroy is called by the Pthread_create function, it will cause it to return an error.
The thread properties are structured as follows:
typedef struct
{
int detachstate; Detach State of Thread
int schedpolicy; Thread Scheduling Policy
struct Sched_param schedparam; Scheduling Parameters for Threads
int inheritsched; Inheritance of threads
int scope; Scope of threads
size_t Guardsize; Alert buffer size at end of thread stack
int stackaddr_set;
void * STACKADDR; Position of line stacks
size_t stacksize; The size of the line stacks
}pthread_attr_t;
Each property is viewed or modified by some function. Below we introduce separately.
Second, the separation state of the thread
the detach state of a thread determines what way a thread terminates itselfin the default case, the thread is in a non detached state., in which case the original thread waits for the thread to be created to end. The created thread is terminated only when the Pthread_join () function returns, in order to release the system resources it occupies.
And the detach thread is not like this,It's not being waited on by another thread, the end of their own run, the thread is terminated, immediately release the system resources. Programmers should choose the appropriate separation state according to their own needs. So if we know that we don't need to know the end state of the thread when we create the thread, we can pthread_attr_t the Detachstate thread property in the structure and let the thread start in a detached state.
2. Name: Pthread_attr_getdetachstate/pthread_attr_setdetachstate
Function: Get/Modify the detach state property of a thread
Header files: #include <pthread.h>
Function prototype: int pthread_attr_getdetachstate (const pthread_attr_t * attr,int *detachstate);
int pthread_attr_setdetachstate (pthread_attr_t *attr,int detachstate);
Parameters: Attr Thread property variable, Detachstate thread's detach state property
Return value: If 0 is returned successfully, if failure returns-1.
You can use the Pthread_attr_setdetachstate function to set the thread property detachstate to one of the following two legal values: set topthread_create_detachedTo detach the state from the start thread, or set topthread_create_joinable, the normal boot thread. You can use the Pthread_attr_getdetachstate function to get the current Datachstate thread properties.
(1) Creating a thread in a detached state
#include <pthread.h>
void *child_thread (void *arg)
{
printf ("Child thread run!\n");
}
int main (int argc,char *argv[])
{
pthread_t Tid;
pthread_attr_t attr;
Pthread_attr_init (&AMP;ATTR);
Pthread_attr_setdetachstate (&attr,pthread_create_detached);
Pthread_create (&AMP;TID,&AMP;ATTR,FN,ARG);
Pthread_attr_destroy (&AMP;ATTR);
Sleep (1);
}
Third, the inheritance of the thread
The functions pthread_attr_setinheritsched and pthread_attr_getinheritsched are used to set and get the inheritance of threads, which are defined as follows:
3. Name: pthread_attr_getinheritsched/pthread_attr_setinheritsched
function: Get/Set the inheritance of the thread
Header files: #include <pthread.h>
Function prototype: int pthread_attr_getinheritsched (const pthread_attr_t *attr,int *inheritsched);
int pthread_attr_setinheritsched (pthread_attr_t *attr,int inheritsched);
Parameters: attr Thread Property variables, inheritsched inheritance of threads
Return value: If 0 is returned successfully, if failure returns-1.
The two functions have two arguments, the 1th is a pointer to the Property object, and the 2nd is an inheritance or pointer to inheritance.inheritance determines whether the parameters of the schedule are inherited from the created process or use the scheduled information explicitly set in the Schedpolicy and Schedparam properties。 Pthreads does not specify a default value for inheritsched, so you must set this property first if you care about the scheduling policy and parameters of the thread.
The possible value of inheritance ispthread_inherit_sched(indicates that the new out-of-the-box will inherit the scheduling policies and parameters of the creation thread) andpthread_explicit_sched(represents the use of scheduling policies and parameters that are explicitly set in the Schedpolicy and Schedparam properties.) If you need to explicitly set a thread's scheduling policy or parameter, you must set the Inheritsched property to pthread_explicit_sched before setting it.

Four, the thread scheduling strategy
The function Pthread_attr_setschedpolicy and pthread_attr_getschedpolicy are used to set and get the scheduling policy of the thread respectively.
4. Name: Pthread_attr_getschedpolicy \pthread_attr_setschedpolicy
function: Get/Set Thread scheduling policy
Header files: #include <pthread.h>
Function prototype: int pthread_attr_getschedpolicy (const pthread_attr_t *attr,int *policy);
int Pthread_attr_setschedpolicy (pthread_attr_t *attr,int policy);
Parameters: attr Thread attribute variable, policy scheduling policy
Return value: If 0 is returned successfully, if failure returns-1.
These two functions have two parameters, the 1th parameter is a pointer to the Property object, and the 2nd parameter is a dispatch policy or a pointer to a dispatch policy. The possible value of a dispatch policy isAdvanced First Out(SCHED_FIFO),Rotation Method(SCHED_RR), or other (Sched_other)。
(1) The SCHED_FIFO policy allows a thread to run until a higher-priority thread is ready, or until it has voluntarily blocked itself. Under the Sched_fifo scheduling strategy, when a thread is ready, it will start executing quickly unless an equal or higher-priority thread is already running.
(2) The SCHED_RR (round robin) strategy is basically the same, except that if a thread with a SCHED_RR policy executes more than a fixed period (the time slice interval) is not blocked, the other sched_rr or schbd_ When a thread of the same priority of the FIPO policy is ready, the running thread will be preempted so that the prepared thread can execute.
When a thread with Sched_fifo or SCHED_RR bribery locks the same mutex with a conditional variable, they are awakened in order of precedence. That is, if a low-priority sched_fifo thread and a high-priority Sched_fifo thread are waiting for the same mutex as the lock, the high priority thread will always be unblocked first when the mutex is unlocked.
V. Scheduling Parameters for Threads
function Pthread_attr_getschedparam and Pthread_attr_setschedparam are used to set and get the scheduling parameters of the thread respectively.
5. Name: Pthread_attr_getschedparam \pthread_attr_setschedparam
function: Get/Set the scheduling parameters of the thread
Header files: #include <pthread.h>
Function prototype: 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);
Parameters: attr thread attribute variable, param sched_param structure
Return value: If 0 is returned successfully, if failure returns-1.
The two functions have two parameters, the 1th argument is a pointer to the Property object, and the 2nd argument is a SCHED_PARAM structure or a pointer to the structure.structure Sched_param in file/usr/include/bits/sched.hare defined in the following:
struct Sched_param
{
int sched_priority;
};
The sched_priority of the structure Sched_param control a priority value, the large priority value corresponds to a high priority. The maximum and minimum priority values supported by the system can be obtained by Sched_get_priority_max function and Sched_get_priority_min function respectively.
Attention:modifying the priority of a thread is not recommended if you are not writing a live program。 Because the scheduling strategy is a very complex thing, if not used correctly can cause program errors, resulting in deadlocks and so on. For example, setting a different priority level for a thread in a multithreaded application may result in a priority inversion due to shared resources.
6. Name: Sched_get_priority_max \sched_get_priority_min
Function: Obtains the maximum and minimum value of the thread priority supported by the system
Header files: #include <pthread.h>
Function prototype: int sched_get_priority_max (int policy); int sched_get_priority_min (int policy);
Parameters: Maximum and minimum values for thread precedence supported by the policy system
Return value: If 0 is returned successfully, if failure returns-1.
Here are some examples of the above functions:
#include <pthread.h>
#include <sched.h>
void *child_thread (void *arg)
{
int policy;
int max_priority,min_priority;
struct Sched_param param;
pthread_attr_t attr;
Pthread_attr_init (&AMP;ATTR); /* Initialize THREAD attribute variable * *
Pthread_attr_setinheritsched (&attr,pthread_explicit_sched); /* Set Thread inheritance * *
Pthread_attr_getinheritsched (&attr,&policy); /* Get the inheritance of the thread * *
if (policy==pthread_explicit_sched)
printf ("inheritsched:pthread_explicit_sched\n");
if (policy==pthread_inherit_sched)
printf ("inheritsched:pthread_inherit_sched\n");
Pthread_attr_setschedpolicy (&AMP;ATTR,SCHED_RR);/* Set Thread scheduling policy * *
Pthread_attr_getschedpolicy (&attr,&policy)//* Get the thread's scheduling policy * *
if (POLICY==SCHED_FIFO)
printf ("schedpolicy:sched_fifo\n");
if (POLICY==SCHED_RR)
printf ("schedpolicy:sched_rr\n");
if (Policy==sched_other)
printf ("schedpolicy:sched_other\n");
Sched_get_priority_max (max_priority)///* Obtain maximum thread priority for system support * *
Sched_get_priority_min (min_priority)///* Get the minimum value of thread priority supported by the system * *
printf ("Max priority:%u\n", max_priority);
printf ("Min priority:%u\n", min_priority);
param.sched_priority=max_priority;
Pthread_attr_setschedparam (&attr,&param);/* Set the scheduling parameters for the thread/*
printf ("sched_priority:%u\n", param.sched_priority);///Get the scheduling parameters of the thread * *
Pthread_attr_destroy (&AMP;ATTR);
}
int main (int argc,char *argv[])
{
pthread_t child_thread_id;
Pthread_create (&child_thread_id,null,child_thread,null);
Pthread_join (Child_thread_id,null);
}

Six, the scope of the thread
The functions Pthread_attr_setscope and pthread_attr_getscope are used to set and get the scope of the thread, which are defined as follows:
7. Name: Pthread_attr_setscope\pthread_attr_getscope
function: Get/Set the scope of the thread
Header files: #include <pthread.h>
Function prototype: int pthread_attr_setscope (pthread_attr_t *attr,int scope);
int Pthread_attr_getscope (const pthread_attr_t *attr,int *scope);
Parameters: attr Thread property variables, scope of the scoped thread
Return value: If 0 is returned successfully, if failure returns-1.
The two functions have two arguments, the 1th is a pointer to the Property object, and the 2nd is a scope or pointer to the scope.The scope controls whether the thread competes for resources within the process or at the system level .The possible values arepthread_scope_process (in-process competitive resources), Pthread_scope_system. (Competing resources at the system level).
Seven, the size of the thread stack
The functions pthread_attr_setstacksize and pthread_attr_getstacksize are used to set and get the size of the thread stack, which are defined as follows:
8. Name: Pthread_attr_getdetstacksize\pthread_attr_setstacksize
Function: Get/Modify the size of line stacks
Header files: #include <pthread.h>
Function prototype: int pthread_attr_getstacksize (const pthread_attr_t *restrict attr,size_t *restrict stacksize);
int Pthread_attr_setstacksize (pthread_attr_t *attr, size_t *stacksize);
Parameters: attr Thread property variable, stacksize stack size
Return value: If 0 is returned successfully, if failure returns-1.
The two parameters have two parameters, the 1th is a pointer to the Property object, and 2nd is the stack size or a pointer to the stack size.using the Pthread_attr_setstacksize function is useful if you want to change the default size of the stack, but do not want to handle the assignment of line stacks yourself .
Eight, the address of the thread stack
The functions pthread_attr_setstackaddr and pthread_attr_getstackaddr are used to set and get the location of the thread stack, which are defined as follows:
9. Name: PTHREAD_ATTR_SETSTACKADDR\PTHREAD_ATTR_GETSTACKADDR
Function: Get/Modify position of line stacks
Header files: #include <pthread.h>
Function prototype: int pthread_attr_getstackaddr (const pthread_attr_t *attr,void **STACKADDF);
int pthread_attr_setstackaddr (pthread_attr_t *attr,void *stackaddr);
Parameters: attr Thread property variable, STACKADDR stack address
Return value: If 0 is returned successfully, if failure returns-1.
The two functions have two arguments, the 1th is a pointer to the Property object, and 2nd is the stack address or a pointer to the stack address.
Nine, the alert buffer size at the end of the thread stack
The function pthread_attr_getguardsize and pthread_attr_setguardsize are respectively used to set and get the alert buffer size at the end of the thread stack, which are defined as follows:
10. Name: Pthread_attr_getguardsize/pthread_attr_setguardsize
Function: Get/Modify the alert buffer size at the end of the thread stack
Header files: #include <pthread.h>
Function prototype: int pthread_attr_getguardsize (const pthread_attr_t *restrict attr,size_t *restrict guardsize);
int Pthread_attr_setguardsize (pthread_attr_t *attr, size_t *guardsize);
Parameters:
Return value: If 0 is returned successfully, if failure returns-1.
    Thread Properties Guardsize control the end of the thread stack to avoid the extended memory size of the stack overflow. This property is set to pagesize bytes by default. The Guardsize thread property can be set to 0, which does not allow this characteristic behavior of the property to occur: The alert buffer is not provided in this case. Similarly, if you modify the thread attribute STACKADDR, the system assumes that we manage the stack ourselves and invalidate the alert stack buffer mechanism, which is equivalent to setting the Guardsize thread property to 0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.