Class mutex

Class Documentation

class mutex

Wrapper for a interprocess pthread based mutex which does not use exceptions!

#include "iceoryx_hoofs/internal/posix_wrapper/mutex.hpp"

int main() {
    posix::mutex myMutex(false);

    myMutex->lock();
    // ... do stuff
    myMutex->unlock();

    {
        std::lock_guard<posix::mutex> lock(myMutex);
        // ...
    }

}
Attention

Errors in c’tor or d’tor can lead to a program termination!

Public Functions

explicit mutex(const bool f_isRecursive) noexcept

Attention

the construction of the mutex can fail. This can lead to a program termination!

~mutex() noexcept

Attention

the destruction of the mutex can fail. This can lead to a program termination!

mutex(const mutex&) = delete

all copy and move assignment methods need to be deleted otherwise undefined behavior or race conditions will occure if you copy or move mutexe when its possible that they are locked or will be locked

mutex(mutex&&) = delete
mutex &operator=(const mutex&) = delete
mutex &operator=(mutex&&) = delete
bool lock() noexcept

Locks the mutex object and returns true if the underlying c function did not returned any error. If the mutex is already locked the method is blocking till the mutex can be locked.

bool unlock() noexcept

Unlocks the mutex object and returns true if the underlying c function did not returned any error. IMPORTANT! Unlocking and unlocked mutex is undefined behavior and the underlying c function will report success in this case!

bool try_lock() noexcept

Tries to lock the mutex object. If it is not possible to lock the mutex object try_lock will return an error. If the c function fails it will return false, otherwise true.

pthread_mutex_t get_native_handle() const noexcept

Returns the native handle which then can be used in pthread_mutex_** calls. Required when a pthread_mutex_** call is not abstracted with this wrapper.

Public Members

pthread_mutex_t m_handle = {}