Thread attribute settings

Source: Internet
Author: User

 

I. Thread attributes


The thread has attributes, which are expressed by pthread_attr_t. Initialization must be performed before the structure is processed. Initialization must be removed after use. We use the pthread_attr_init function to initialize it, and use pthread_attr_destroy to remove initialization from it.

 

1.

Name ::

Pthread_attr_init/pthread_attr_destroy

Function:

Initialize/remove initialization of thread attributes

Header file:

# Include <pthread. h>

Original function:

Int pthread_attr_init (pthread_attr_t * ATTR );

Int pthread_attr_destroy (pthread_attr_t * ATTR );

Parameters:

ATTR
Thread attribute variable

Return Value:

If the request is successful, 0 is returned. If the request fails,-1 is returned.

 

 

 

After pthread_attr_init is called, the content contained in the pthread_t structure is the default value of all attributes of the supported threads implemented by the operating system.


To delete the initialization of the pthread_attr_t structure, you can call the pthread_attr_destroy function. If pthread_attr_init allocates dynamic memory space for the property object during implementation, pthread_attr_destroy will also use invalid values to initialize the property object. Therefore, if the pthread_attr_destroy removes the initialized pthread_attr_t structure, it will be called by P, it will cause an error.

 

The thread attribute structure is as follows:

Typedef struct

{

Int detachstate; separation state of threads

Int schedpolicy;
Thread Scheduling Policy

Struct sched_param schedparam;
Thread Scheduling Parameters

Int inheritsched;
Thread inheritance

Int scope;
Thread Scope

Size_t guardsize;
Alert buffer size at the end of the thread Stack

Int stackaddr_set;

Void * stackaddr;
Thread stack location

Size_t stacksize;
Thread stack size

} Pthread_attr_t;

 

Each attribute corresponds to some functions to view or modify it. Next we will introduce them separately.

 

Ii. Separation of threads


The separation status of a thread determines how a thread terminates itself. By default, threads are not separated. In this case, the original thread waits for the creation of the thread to end. Only when the pthread_join () function returns, the created thread is terminated and the system resources occupied by it can be released.

The separation thread is not like this. It is not waiting by other threads. When the running ends, the thread is terminated and system resources are released immediately. Programmers should select appropriate separation States based on their own needs. Therefore, if we know that we do not need to know the thread termination status when creating a thread, we can use the detachstate thread attribute in the pthread_attr_t structure to enable the thread to start in a separate state.

 

2.

Name ::

Pthread_attr_getdetachstate/pthread_attr_setdetachstate

Function:

Obtain/modify the separation state attribute of a thread

Header file:

# Include <pthread. h>

Original function:

Int pthread_attr_getdetachstate (const pthread_attr_t * ATTR, int * detachstate );

Int pthread_attr_setdetachstate (pthread_attr_t * ATTR, int detachstate );

Parameters:

ATTR
Thread attribute variable

Detachstate
Separation status attributes of threads

Return Value:

If the request is successful, 0 is returned. If the request fails,-1 is returned.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

You can use the pthread_attr_setdetachstate function to set the thread attribute detachstate to one of the following valid values: Set it to pthread_create_detached and start the thread in a separate state; or set it to pthread_create_joinable to start the thread normally. You can use the pthread_attr_getdetachstate function to obtain the current datachstate thread attribute.

 

Creates a thread in a separate state.

# Iinclude <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 (& ATTR );

Pthread_attr_setdetachstate (& ATTR, pthread_create_detached );

Pthread_create (& tid, & ATTR, FN, ARG );

Pthread_attr_destroy (& ATTR );

Sleep (1 );

}

 

Iii. Thread inheritance


The pthread_attr_setinheritsched and pthread_attr_getinheritsched functions are respectively used to set and obtain thread inheritance. The two functions are defined as follows:

 

3.

Name ::

Pthread_attr_getinheritsched

Pthread_attr_setinheritsched

Function:

Obtain/set thread inheritance

Header file:

# Include <pthread. h>

Original function:

Int pthread_attr_getinheritsched (const pthread_attr_t * ATTR, int * inheritsched );

Int pthread_attr_setinheritsched (pthread_attr_t * ATTR, int inheritsched );

Parameters:

ATTR thread attribute variable

Inheritsched
Thread inheritance

Return Value:

If the request is successful, 0 is returned. If the request fails,-1 is returned.

 

 

 

 

 

 

 

 

These two functions have two parameters, 1st are pointers to property objects, and 2nd are inherited or inherited pointers. Inheritance determines whether the scheduling parameter is inherited from the created process or explicitly set in the schedpolicy and schedparam attributes. Pthreads does not specify the default value for inheritsched. Therefore, if you are concerned about Thread Scheduling Policies and parameters, you must set this attribute first.


The possible values of inheritance are pthread_inherit_sched (indicating that the new generation will inherit the Scheduling Policies and parameters of the created thread) and pthread_explicit_sched (indicating that the scheduling policies and parameters explicitly set in the schedpolicy and schedparam attributes are used ).


If you need to explicitly set a thread scheduling policy or parameter, you must set the inheritsched attribute to pthread_explicit_sched before setting it.


The following describes the process scheduling policies and scheduling parameters. I will give the program example of this function in combination with the following function.

Copy From: http://blogold.chinaunix.net/u/22935/showart_341886.html

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.