carb::thread::mutex

Defined in carb/thread/Mutex.h

class mutex : protected detail::BaseMutex<false>

A Carbonite implementation of std::mutex.

Note

Windows: std::mutex uses SRWLOCK for Win 7+, CONDITION_VARIABLE for Vista and the massive CRITICAL_SECTION for pre-Vista. Due to this, the std::mutex class is about 80 bytes. Since Carbonite supports Windows 10 and later, SRWLOCK is used exclusively. This implementation is 16 bytes. This version that uses SRWLOCK is significantly faster on Windows than the portable implementation used for the linux version.

Note

Linux: sizeof(std::mutex) is 40 bytes on GLIBC 2.27, which is based on the size of pthread_mutex_t. The Carbonite implementation of mutex is 16 bytes and at least as performant as std::mutex in the contended case.

Public Functions

mutex() noexcept = default

Constructor.

Note

Unlike std::mutex, this implementation can be declared constexpr.

~mutex() = default

Destructor.

Warning

std::terminate() is called if mutex is locked by a thread.

inline void lock()

Locks the mutex, blocking until it becomes available.

Warning

std::terminate() is called if the calling thread already has the mutex locked. Use recursive_mutex if recursive locking is desired. The calling thread must call unlock() at a later time to release the lock.

inline bool try_lock()

Attempts to immediately lock the mutex.

Warning

std::terminate() is called if the calling thread already has the mutex locked. Use recursive_mutex if recursive locking is desired.

Returns

true if the mutex was available and the lock was taken by the calling thread (unlock() must be called from the calling thread at a later time to release the lock); false if the mutex could not be locked by the calling thread.

inline void unlock()

Unlocks the mutex.

Warning

std::terminate() is called if the calling thread does not own the mutex.

inline bool is_current_thread_owner() const noexcept

Checks if the current thread owns the mutex.

Note

This is a non-standard Carbonite extension.

Returns

true if the current thread owns the mutex; false otherwise.