1. Thread Properties
You can use the pthread_attr_t structure to specify the properties of a thread and associate those attributes with the thread. You can use the Pthread_attr_init function to initialize the default value of the Pthread_attr_t object as a system thread property.
Given the thread properties defined in each system.
If you are not interested in the terminating state of a thread, you can modify the Detachstate property in the pthread_attr_t structure so that the thread is separated from the beginning, and can be used pthread_attr_ Setdetachstate (const pthread_attr_t *restrict attr,int * detachstate) changes the properties of the pthread_attr_t structure.
can be The thread's stack properties are managed through the Pthread_attr_getstack function and the Pthread_attr_setstack function.
Where stackaddr represents the lowest address of the stack, stacksize represents the size of the stack.
If you want to change the default stack size, and do not want to deal with the distribution of line stacks problem, then use pthread_attr_setstacksize is appropriate. When Stack_size is selected, Stack_size cannot be less than pthread_stack_min.
Properties with two threads are not included in the pthread_attr_t structure, they are both de-selectable and can be canceled.
The cancellation state can be either pthread_cancel_enable or pthread_cancel_disable. A thread can modify its de-pthread_setcancelstate state by using an. Enabled when a thread starts by default when it is canceled. When the status is disable, calls to Pthread_cancel on the thread do not kill the thread. At this point the cancellation request is still pending for this thread, and when the cancellation state becomes pthread_cancel_enable again, the thread will process the pending cancellation request on the next cancellation point (by default, the thread will continue to run after the cancellation request has been issued. Until a thread reaches a cancellation point).
The default cancellation type for the system is deferred cancellation, which means that the thread is not actually canceled until the cancel point is reached. We can set the type of cancellation by Pthread_setcanceltype. The optional types are pthread_cancel_deferred,pthread_cancel_asynchronous. When Pthread_cancel_asynchronous is selected , the thread can be canceled at any one time without waiting for the cancel point.
2. Mutex Property
The mutex property is represented by the pthread_mutexattr_t structure. It is noteworthy that the three properties are: Process shared properties, robust properties, and type properties.
(1) Process sharing properties
Synchronization is required when the process shares data. When the process share mutex property is set to pthread_process_shared, it can be used for synchronization of these processes.
Use the pthread_mutexattr_getshared function to get the shared properties, and the Pthread_mutexattr_setshared function to set the shared properties.
(2) Robustness
The mutex robustness attribute is related to the sharing of mutexes between multiple processes. This means that when the process that holds the mutex is terminated, the problem of the mutex state recovery is resolved.
There are two different possible values for a robust property. The default value is pthread_mutex_stalled, which means that the process holding the mutex does not need to take additional action at the time of termination. In this case, the behavior after using the mutex is undefined, and the application waiting for the mutex to be unlocked is stalled. The other value is pthread_mutex_robust. When the robustness of the mutex is set to pthread_mutex_robust, if the thread calls Pthread_mutex_lock to acquire the lock, and the lock is held by another process, but the process terminates without releasing the lock, the thread blocks, But the return value would be eownerdead instead of 0.
(3) Type attribute
POSIX.1 defines four types: (1) Pthread_mutex_normal: A standard mutex type that does not detect any errors. (2)Pthread_mutex_errorcheck: Provide error checking for unreasonable operation. (3)pthread_mutex_recursive: Allows the mutex to be locked multiple times without releasing the mutex. (4)Pthread_mutex_default: Provides a default behavior that the operating system maps to other mutexes when it is implemented.
These four types of behavior are shown:
No occupancy is the case when unlocking represents a thread that unlocks the mutex of another thread when unlocked to indicate when a thread unlocks a mutex that has been unlocked.
the type of mutex can be managed through the Pthread_mutexattr_gettype function and the Pthread_mutexattr_settype function.
3. Read-Write Lock properties
The only property supported by the read-write lock is the process share property, which is the same as the mutex process share property and can be manipulated by the appropriate get and set functions.
4. Barrier Properties
The only property supported by the barrier is the process share property, which is the same as the mutex process share property and can be manipulated by the corresponding get and set functions.
Advanced Programming for UNIX environment thread of reading notes (2)