Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

4.3. Mutex Options


Procedure 4.1. Standard Mutex Creation

Mutual exclusion (mutex) algorithms are used to prevent processes simultaneously using a common resource. A fast user-space mutex (futex) is a tool that allows a user-space thread to claim a mutex without requiring a context switch to kernel space, provided the mutex is not already held by another thread.

Note

In this document, we use the terms futex and mutex to describe POSIX thread (pthread) mutex constructs.
  1. When you initialize a pthread_mutex_t object with the standard attributes, it will create a private, non-recursive, non-robust and non priority inheritance capable mutex.
  2. Under pthreads, mutexes can be initialized with the following strings:
    pthread_mutex_t my_mutex;
    
    pthread_mutex_init(&my_mutex, NULL);
    
    Copy to Clipboard Toggle word wrap
  3. 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

In order to define any additional capabilities for the mutex you will need to create a pthread_mutexattr_t object. This object will store the defined attributes for the futex.

Important

For the sake of brevity, these examples do not include a check of the return value of the function. This is a basic safety procedure and one that you must always perform.
  1. Creating the mutex object:
    pthread_mutex_t my_mutex;
    
    pthread_mutexattr_t my_mutex_attr;
    
    pthread_mutexattr_init(&my_mutex_attr);
    
    Copy to Clipboard Toggle word wrap
  2. Shared and Private mutexes:
    Shared mutexes can be used between processes, however they can create a lot more overhead.
    pthread_mutexattr_setpshared(&my_mutex_attr, PTHREAD_PROCESS_SHARED);
    
    Copy to Clipboard Toggle word wrap
  3. Real-time priority inheritance:
    Priority inversion problems can be avoided by using priority inheritance.
    pthread_mutexattr_setprotocol(&my_mutex_attr, PTHREAD_PRIO_INHERIT);
    
    Copy to Clipboard Toggle word wrap
  4. Robust mutexes:
    Robust mutexes are released when the owner dies, however this can also come at a high overhead cost. _NP in this string indicates that this option is non-POSIX or not portable.
    pthread_mutexattr_setrobust_np(&my_mutex_attr, PTHREAD_MUTEX_ROBUST_NP);
    
    Copy to Clipboard Toggle word wrap
  5. Mutex initialization:
    Once the attributes are set, initialize a mutex using those properties.
    pthread_mutex_init(&my_mutex, &my_mutex_attr);
    
    Copy to Clipboard Toggle word wrap
  6. 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 _destroy command.
    pthread_mutexattr_destroy(&my_mutex_attr);
    
    Copy to Clipboard Toggle word wrap
    The mutex will now operate as a regular pthread_mutex, and can be locked, unlocked and destroyed as normal.
Related Manual Pages

For more information, or for further reading, the following man pages are related to the information given in this section.

  • futex(7)
  • pthread_mutex_destroy(P)
    For information on pthread_mutex_t and pthread_mutex_init
  • pthread_mutexattr_setprotocol(3p)
    For information on pthread_mutexattr_setprotocol and pthread_mutexattr_getprotocol
  • pthread_mutexattr_setprioceiling(3p)
    For information on pthread_mutexattr_setprioceiling and pthread_mutexattr_getprioceiling
Nach oben
Red Hat logoGithubredditYoutubeTwitter

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

Theme

© 2025 Red Hat