Evo C++ Library v0.5.1
Public Types | Public Member Functions | List of all members
MutexRW Struct Reference

#include <evo/thread.h>

Detailed Description

Read/Write Mutex for thread synchronization.

Public Types

typedef SmartLock< MutexRWLock
 Write Lock object type, general Mutex interface (Mutex::Lock will also work) – see SmartLock. More...
 
typedef SmartLockRead< MutexRWLockRead
 Read Lock object type – see SmartLockRead. More...
 
typedef SmartLock< MutexRWLockWrite
 Write Lock object type – see SmartLock. More...
 

Public Member Functions

 MutexRW ()
 Constructor. More...
 
 ~MutexRW ()
 Destructor. More...
 
void lock ()
 Write-Lock mutex. More...
 
void lock_read ()
 Read-Lock mutex. More...
 
bool trylock ()
 Try to Write-Lock mutex without blocking. More...
 
bool trylock (ulong timeout_ms)
 Try to Write-Lock mutex with a timeout. More...
 
bool trylock_read ()
 Try to Read-Lock mutex without blocking. More...
 
void unlock ()
 Write-Unlock mutex. More...
 
void unlock_read ()
 Read-Unlock mutex. More...
 

Member Typedef Documentation

◆ Lock

Write Lock object type, general Mutex interface (Mutex::Lock will also work) – see SmartLock.

◆ LockRead

Read Lock object type – see SmartLockRead.

◆ LockWrite

Write Lock object type – see SmartLock.

Constructor & Destructor Documentation

◆ MutexRW()

MutexRW ( )
inline

Constructor.

◆ ~MutexRW()

~MutexRW ( )
inline

Destructor.

Member Function Documentation

◆ lock()

void lock ( )
inline

Write-Lock mutex.

  • Must call unlock() after each lock(), otherwise results are undefined
  • Results are undefined if already write-locked by current thread

◆ lock_read()

void lock_read ( )
inline

Read-Lock mutex.

  • Must call unlock_read() after each lock_read(), otherwise results are undefined
  • Results are undefined if already read-locked by current thread

◆ trylock() [1/2]

bool trylock ( )
inline

Try to Write-Lock mutex without blocking.

  • This allows polling for a write-lock without blocking
  • If current thread already has a lock, whether this succeeds is platform dependent – some platforms (Windows) allow nested locks, others don't
  • Caution: Polling with this can starve (never lock) under high load (constant read/write locks)
Returns
Whether successful, false if write-lock not available

◆ trylock() [2/2]

bool trylock ( ulong  timeout_ms)
inline

Try to Write-Lock mutex with a timeout.

  • This allows polling for a write-lock until timeout
  • If current thread already has a lock, whether this succeeds is platform dependent – some platforms (Windows) allow nested locks, others don't
  • OSX: This does a spin wait (which consumes CPU) since OSX doesn't support timeout on pthread mutex lock
  • Windows: This does a spin wait (which consumes CPU) since Windows doesn't support timeout on Critical Section lock
  • Caution: This can starve (never lock) in Windows or OSX under high load (constant locks)
Parameters
timeout_msTimeout in milliseconds
Returns
Whether successful, false on timeout

◆ trylock_read()

bool trylock_read ( )
inline

Try to Read-Lock mutex without blocking.

  • This allows polling for a read-lock without blocking
  • If current thread already has a lock, whether this succeeds is platform dependent – some platforms (Windows) allow nested locks, others don't
  • Caution: Polling with this can starve (never lock) under high load (constant read/write locks)
Returns
Whether successful, false if read-lock not available

◆ unlock()

void unlock ( )
inline

Write-Unlock mutex.

  • Results are undefined if called while mutex not write-locked

◆ unlock_read()

void unlock_read ( )
inline

Read-Unlock mutex.

  • Results are undefined if called while mutex not read-locked

The documentation for this struct was generated from the following file: