Index:
1. initialize the property object pthread_attr_init
2. Delete the property object pthread_attr_destroy.
3. Set the separation status pthread_attr_setdetachstate
4. Get the separation status pthread_attr_getdetachstate
5. Set the size of the stack protection area pthread_attr_setguardsize
6. Obtain the size of the stack protection area pthread_attr_getguardsize
7. Set the domain pthread_attr_setscope
8. Obtain the pthread_attr_getscope domain.
9. Set the concurrency level pthread_setconcurrency
10. Get the concurrency level pthread_getconcurrency
11. Set the scheduling policy pthread_attr_setschedpolicy
12. Get the scheduling policy pthread_attr_getschedpolicy
13. Set the inherited scheduling policy pthread_attr_setinheritsched
14. Get the inherited scheduling policy pthread_attr_getinheritsched
15. Set the scheduling parameter pthread_attr_setschedparam
16. Obtain the scheduling parameter pthread_attr_getschedparam.
17. Set the stack size pthread_attr_setstacksize
18. Get the stack size pthread_attr_getstacksize
19. Set the stack base address pthread_attr_setstackaddr
20. Get the stack base address pthread_attr_getstackaddr
The following is the text
1. initialize the property object pthread_attr_init
# Include <pthread. h>
Int pthread_attr_init (pthread_attr_t * ATTR );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Initialize a thread property object and set its property value to the default value. The memory occupied by the thread library is allocated.
The default values of attribute objects are as follows:
| Attribute |
Value |
Description |
| Scope (thread domain) |
Pthread_scope_process |
The thread is not bound, that is, it is not permanently bound to lwp. |
| Detachstate) |
Pthread_create_joinable |
After the thread exits, the thread exit code and thread will be temporarily retained |
| Stackaddr (stack address) |
Null |
The thread stack is allocated by the system. |
| Stacksize (stack size) |
1 M bytes |
The stack size of a thread is determined by the system. |
| Priority (priority) |
|
The thread inherits the thread priority from the parent thread. |
| Inheritsched (inherited scheduling priority) |
Pthread_inherit_sched |
The thread inherits the scheduling priority of the parent thread. |
| Schedpolicy) |
Sched_other |
The thread is scheduled based on the priority, and the thread remains running until the higher-priority thread occupies the CPU resources, or the thread is blocked, or the thread actively grants the running permission. |
2. Delete the property object pthread_attr_destroy.
# Include <pthread. h>
Int pthread_attr_destroy (pthread_attr_t * ATTR );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Used to delete the memory occupied by attribute objects. After this function is called, the corresponding property object is invalid.
3. Set the separation status pthread_attr_setdetachstate
# Include <pthread. h>
Int pthread_attr_setdetachstate (pthread_attr_t * ATTR, int detachstate );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Set the separation status. The value of detachstate is pthread_create_detached and pthread_create_joinable.
NOTE: If there are no valid synchronization measures, a newly created separation thread may be terminated before the pthread_create () function returns, its thread identifier is used by another new thread.
4. Get the separation status pthread_attr_getdetachstate
# Include <pthread. h>
Int pthread_attr_getdetachstate (pthread_attr_t * ATTR, int * detachstate );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Take the thread separation status: separated or non-separated.
5. Set the size of the stack protection area pthread_attr_setguardsize
# Include <pthread. h>
Int pthread_attr_setguardsize (pthread_attr_t * ATTR, size_t guardsize );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Set the guardsize parameter of the stack protection area of the thread property object.
If the value of the guardsize parameter of the thread property object is 0, no stack protection zone will be created when a thread is created. If the value of the guardsize parameter of the thread property object is greater than 0, the thread created using this thread property object will have at least a guardsize stack reserve.
By default, the stack reserve size of a thread varies with the system version.
When creating a thread stack, the general practice is to round the value of the guardsize parameter to an integer multiple of the system variable pagesize to allocate a stack reserve.
Although the actual size of the stack reserve is usually an integer multiple of the rounded up value and pagesize of the guardsize parameter. However, the program calls the pthread_attr_getguardsize () function to obtain the size of the stack reserve. The size of the stack reserve will be the value of the guardsize parameter specified when the pthread_attr_setguardsize () function is called.
6. Obtain the size of the stack protection area pthread_attr_getguardsize
# Include <pthread. h>
Int pthread_attr_getguardsize (const pthread_attr_t * ATTR, size_t * guardsize );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
The size of the stack protected area of the thread property object.
The size of the stack protected area is the guardsize parameter value specified when the pthread_attr_setguardsize () function is called.
7. Set the domain pthread_attr_setscope
# Include <pthread. h>
Int pthread_attr_setscope (pthread_attr_t * tattr, int scope );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Set the domain. Specify whether the thread to be created is bound (pthread_scope_system) or not (pthread_scope_process ).
There can be two different types of threads in a process at the same time.
8. Obtain the pthread_attr_getscope domain.
# Include <pthread. h>
Int pthread_attr_getscope (pthread_attr_t * tattr, int * scope );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Take the thread's domain (bound or not bound ).
9. Set the concurrency level pthread_setconcurrency
# Include <pthread. h>
Int pthread_setconcurrency (INT new_level );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Sets the thread concurrency level.
In a process, multiple unbound threads may be activated at the same time (bound to lwp for core concurrency scheduling ). By default, the thread library ensures that there are enough threads in the active State to ensure that the process is running. Although this can save system resources, it is not necessarily the most effective concurrency.
This function allows an application to notify the thread library of the parallel level required by it (that is, the maximum number of non-bound threads at the same time point can be activated ). But it only notifies the System of the concurrency level it requires. The system uses it as a prompt, not a command.
If the value of new_level is 0, the parallel level remains unchanged, just as this function has never been called.
10. Get the concurrency level pthread_getconcurrency
# Include <pthread. h>
Int pthread_getconcurrency (void );
Obtain the thread concurrency level.
Returns the value originally set by the pthread_setconcurrency () function. If the pthread_setconcurrency () function has not been called before, the pthread_getconcurrency () function returns 0, which indicates that the thread Library maintains the concurrency level.
11. Set the scheduling policy pthread_attr_setschedpolicy
# Include <pthread. h>
Int pthread_attr_setschedpolicy (pthread_attr_t * tattr, int policy );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Set the scheduling policy.
POSIX standard-defined scheduling policies include sched_fifo (first-in-first-out), sched_rr (loop), and sched_other (default scheduling policies defined by POSIX thread libraries of different versions ).
12. Get the scheduling policy pthread_attr_getschedpolicy
# Include <pthread. h>
Int pthread_attr_getschedpolicy (pthread_attr_t * tattr, int * policy );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Take the scheduling policy.
13. Set the inherited scheduling policy pthread_attr_setinheritsched
# Include <pthread. h>
Int pthread_attr_setinheritsched (pthread_attr_t * tattr, int inherit );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Sets an inherited scheduling policy.
If the value of the inherit parameter is pthread_inherit_sched (default value), All threads created using this property object will have the scheduling policy of the parent thread (the scheduling policy parameter in the property object will not work ). If the value of the inherit parameter is pthread_explicit_sched, when a thread is created using this attribute object, the scheduling policy of the thread is irrelevant to the parent thread, and the scheduling policy parameter in the attribute object takes effect.
14. Get the inherited scheduling policy pthread_attr_getinheritsched
# Include <pthread. h>
Int pthread_attr_getinheritsched (pthread_attr_t * tattr, int * inherit );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Obtain the inherited scheduling policy.
15. Set the scheduling parameter pthread_attr_setschedparam
# Include <pthread. h>
Int pthread_attr_setschedparam (pthread_attr_t * tattr, const struct sched_param * PARAM );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Set scheduling parameters.
The scheduling parameters of attribute objects are defined in the param structure. In this structure, only priority members are defined. The priority of the newly created thread is specified by the priority parameter of the param structure in the property object.
There are two ways to modify the priority of a thread. You can set the priority parameter of the property object before creating a child thread. You can also modify the priority of the parent thread and then create a child thread.
The sched_param structure may store other scheduling information. Therefore, it is a good habit to obtain the existing scheduling parameters before modifying the scheduling parameters of the thread attribute object. A reasonable code should be like this: first, take the existing scheduling parameters in the thread attribute object, perform operations on the retrieved scheduling parameters, and reset the thread attribute object with the modified scheduling parameters.
16. Obtain the scheduling parameter pthread_attr_getschedparam.
# Include <pthread. h>
Int pthread_attr_getschedparam (pthread_attr_t * tattr, const struct sched_param * PARAM );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Obtain scheduling parameters.
17. Set the stack size pthread_attr_setstacksize
# Include <pthread. h>
Int pthread_attr_setstacksize (pthread_attr_t * tattr, int size );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Set the stack size.
The size parameter indicates the size of the stack required by the thread. If the value of size is 0. The default size is used. In most cases, the default value is the most appropriate. The stack size cannot be smaller than the minimum stack capacity defined by the system.
Pthread_stack_min is the minimum stack size required to start a thread. This does not include the stack required to execute the code in this thread.
The stacksize of the attribute object defines the size (in bytes) of the stack allocated by the system for the thread ).
18. Get the stack size pthread_attr_getstacksize
# Include <pthread. h>
Int pthread_attr_getstacksize (pthread_attr_t * tattr, size_t * size );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Returns the thread stack size set by the pthread_attr_setstacksize () function.
19. Set the stack base address pthread_attr_setstackaddr
# Include <pthread. h>
Int pthread_attr_setstackaddr (pthread_attr_t * tattr, void * stackaddr );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Set the base address of the thread stack.
If stackaddr is set to a non-null value (the default value is a null pointer), the thread stack starts from here. If it is null, pthread_create () allocates a stack for the thread.
20. Get the stack base address pthread_attr_getstackaddr
# Include <pthread. h>
Int pthread_attr_getstackaddr (pthread_attr_t * tattr, void ** stackaddr );
Return Value: 0 is returned if the function is successful. Any other return values indicate errors.
Returns the stack address set by the pthread_attr_setstackaddr () function.