forksafe_mutex_init()

Updated: April 19, 2023

Initialize a forksafe mutex

Synopsis:

#include <forksafe_mutex.h>

forksafe_mutex_t mutex = FORKSAFE_MUTEX_INITIALIZER;

int forksafe_mutex_init(
            forksafe_mutex_t* mutex,
            const pthread_mutexattr_t* attr );

Arguments:

mutex
A pointer to the forksafe_mutex_t object that you want to initialize.
Note: It's always safe, and typically faster, to assure that mutex is 32-bit aligned.
attr
NULL, or a pointer to a pthread_mutexattr_t object that specifies the attributes that you want to use for the mutex. For more information, see pthread_mutexattr_init().

Library:

libc, libforksafe_mutex

In QNX Neutrino 7.1 or later, there are two versions of the forksafe_mutex_*() functions:

Description:

The forksafe_mutex_init() function initializes the given forksafe mutex object, using the attributes specified by the mutex attributes object attr. If attr is NULL, then the mutex is initialized with the default attributes (see pthread_mutexattr_init()). After initialization, the mutex is in an unlocked state.

CAUTION:
You should allocate synchronization objects only in normal memory mappings. On certain processors, atomic operations such as calls to forksafe_mutex_lock() will cause a fault if the control structure is allocated in uncached memory.

You can initialize a statically allocated mutex with the default attributes by assigning to it the macro FORKSAFE_MUTEX_INITIALIZER or FORKSAFE_RMUTEX_INITIALIZER (for recursive mutexes).

Note: To destroy a mutex, call forksafe_mutex_destroy(). Once you've destroyed a mutex, don't reuse it without reinitializing it by calling forksafe_mutex_init().

For more information about forksafe mutexes, see Using fork() in a multithreaded process in the “Processes and Threads” chapter of Getting Started with QNX Neutrino.

Returns:

EOK
Success.
EFAULT
A fault occurred when the kernel tried to access mutex or attr.
EINVAL
The value specified by attr is invalid.
ENOMEM
All kernel synchronization objects are in use.

Classification:

QNX Neutrino

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes