mutex#
Fully qualified name: carb::thread::mutex
Defined in carb/thread/Mutex.h
-
class mutex : protected detail::BaseMutex<false>#
A Carbonite implementation of std::mutex.
Note
Windows:
std::mutexusesSRWLOCKfor Win 7+,CONDITION_VARIABLEfor Vista and the massiveCRITICAL_SECTIONfor pre-Vista. Due to this, thestd::mutexclass is about 80 bytes. Since Carbonite supports Windows 10 and later,SRWLOCKis used exclusively. This implementation is 16 bytes. This version that usesSRWLOCKis 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 ofpthread_mutex_t. The Carbonite implementation of mutex is 16 bytes and at least as performant asstd::mutexin the contended case.Public Functions
-
mutex() noexcept = default#
Constructor.
Note
Unlike
std::mutex, this implementation can be declaredconstexpr.
-
~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:
trueif 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);falseif 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:
trueif the current thread owns the mutex;falseotherwise.
-
mutex() noexcept = default#