4.3. Mutex Options
Procedure 4.1. Standard Mutex Creation
- When you initialize a
pthread_mutex_tobject with the standard attributes, it will create a private, non-recursive, non-robust and non priority inheritance capable mutex.
- Under pthreads, mutexes can be initialized with the following strings:
pthread_mutex_t my_mutex; pthread_mutex_init(&my_mutex, NULL);
- In this case, your application will not benefit from the advantages provided by the pthreads API and the Red Hat Enterprise Linux for Real Time kernel. There are a number of mutex options that must be considered when writing or porting an application.
Procedure 4.2. Advanced Mutex Options
pthread_mutexattr_tobject. This object will store the defined attributes for the futex.
- Creating the mutex object:
pthread_mutex_t my_mutex; pthread_mutexattr_t my_mutex_attr; pthread_mutexattr_init(&my_mutex_attr);
- Shared and Private mutexes:Shared mutexes can be used between processes, however they can create a lot more overhead.
- Real-time priority inheritance:Priority inversion problems can be avoided by using priority inheritance.
- Robust mutexes:Robust mutexes are released when the owner dies, however this can also come at a high overhead cost.
_NPin this string indicates that this option is non-POSIX or not portable.
- Mutex initialization:Once the attributes are set, initialize a mutex using those properties.
- Cleaning up the attributes object:After the mutex has been created, you can keep the attribute object in order to initialize more mutexes of the same type, or you can clean it up. The mutex is not affected in either case. To clean up the attribute object, use the
pthread_mutexattr_destroy(&my_mutex_attr);The mutex will now operate as a regular
pthread_mutex, and can be locked, unlocked and destroyed as normal.
For more information, or for further reading, the following man pages are related to the information given in this section.
- pthread_mutex_destroy(P)For information on
- pthread_mutexattr_setprotocol(3p)For information on
- pthread_mutexattr_setprioceiling(3p)For information on