Apue Learning Multi-Threading Programming (III): Thread Properties, synchronization properties

Source: Internet
Author: User
Tags mutex

One, thread properties can use the pthread_attr_t structure to modify the thread default properties, and these properties and the created thread practice, you can use the Pthread_att_init function to initialize the pthread_attr_t structure, call Pthread_attr_     After Init, the pthread_attr_t structure contains the default values for all the thread properties supported by the operating system implementation. Pthread_attr_destroy is used to destroy property objects and to free resources.
#include <pthread.h>int pthread_attr_init (pthread_attr_t *attr)int pthread_attr_ Destroy (pthread_attr_t *attr)

There are four thread properties:

1.detachstate Thread's Detach state property 2.guardsize thread stack at the end of the alert buffer size (bytes) 3.stackaddr line stacks minimum address 4.stacksize line stacks minimum length if the thread is created Knowing that you do not need to know 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. You can use Pthread_attr_setdetachstate to set the thread property detachstate to one of the following two legal values: pthread_create_detached,pthread_create_joinable.
#include <pthread.h>int pthread_attr_getdetachstate (constint *detachstate); int pthread_attr_setdetachstate (constint *detachstate);

Example:

#include"apue.h"#include<pthread.h>intMakethread (void* (*FN) (void*),void*Arg) {    interr;    pthread_t Tid;     pthread_attr_t attr; Err= Pthread_attr_init (&attr); if(Err! =0)    {        returnerr; } Err= Pthread_attr_setdetachstate (&attr, pthread_create_detached); if(Err = =0) {Err= Pthread_create (&tid, &attr, FN, ARG); } Pthread_attr_destroy (&attr); returnerr;}
View CodeSystems that follow the POSIX standard do not necessarily support thread stack properties, and you can use the _POSIX_THREAD_ATTR_STACKADDR and _posix_thread_attr_stacksize symbols in the compile phase to check whether the system supports thread stack properties. Thread stack properties can be managed using Pthread_attr_getstack and Pthread_attr_setstack
#include <pthread.h>int pthread_attr_getstack (constvoid **restrick stackaddr, size _t *restrict stacksize)intvoid *stackaddr, size_t stacksize)

If the virtual address space of the line stacks is exhausted, then malloc or mmap can be used to allocate space for an alternative stack, stackaddr the lowest memory address of the stack.

You can also read or set thread properties stacksize through Pthread_attr_getstacksize and pthread_attr_setstacksize.
#include <pthread.h>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)

The Thread property Guardsize controls the size of the extended memory that is used to avoid stack overflow after the end of the thread stack.

#include <phtread.h>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)

Second, the mutex attribute

Corresponds to a non-default property, which can be initialized using Pthread_mutexattr_init, Pthread_mutexattr_destroy.
#include <pthread.h>int pthread_mutexattr_init (pthread_mutexattr *attr)int Pthread_mutexattr_destroy (pthread_mutexattr *attr)

Notable two properties in a mutex attribute: Process share property, type property

When the process share property is set to pthread_process_shared, multiple processes are allowed to access the shared data, which is not allowed when the process share property is pthread_process_private.
#include <pthread.h>int pthread_mutexattr_getpshared (constint *Restrict pshared)int pthread_mutexattr_setpshared (constint pshared)

The type attribute controls the locking characteristics of the mutex, notably the Pthread_mutex_recursive type, which allows the same thread to lock the mutex multiple times before the mutex is unlocked. The cardinality of the lock is maintained by the recursive mutex, and is not unlocked in the case of the number of unlocks and the number of locks.

#include <pthread.h>int pthread_mutexattr_gettype (constint *restrict type)  intint type)

Third, read-write lock properties

Use Pthread_rwlockattr_init to initialize the pthread_rwlockattr_t structure and destroy it with Pthread_rwlockattr_destroy.
#include <pthread.h>int pthread_rwlockattr_init (pthread_rwlockattr_t *attr)int Pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr)

The read-write lock Unique property is the process share property, which is the same as the mutex process share property.

#include <pthread.h>int pthread_rwlockattr_getpshared (constint *Restrict pshared)int pthread_rwlockattr_setpshared (constint * pshared)

Iv. Conditional Variable Properties

There is a pair of functions for initialization and destruction.
#include <pthread.h>int pthread_condattr_init (pthread_condattr_t *attr)int Pthread_condattr_destroy (pthread_condattr_t *attr)

The condition variable supports the process share property and the clock property, where the process share property is the same as the mutex's process share property.

#include <pthread.h>int pthread_condattr_getpshared (constint *Restrict pshared)int pthread_condattr_setpshared (constint pshared)

The Clock property controls the time-out parameter of the pthread_cond_timedwait function tsptr which clock is used.

#include <pthread.h>int pthread_condattr_getclock (const pthread_condattr_t *restrict attr, clockid_t *restrict clock_id)int pthread_condattr_setclock (pthread_condattr_t *attr, clockid_t CLOCK_ID)

Apue Learning Multi-Threading Programming (III): Thread Properties, synchronization properties

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.