std::mutex And Friends

std::mutex

  • Simplest mutual exclusion device

  • Can be taken only once

  • Not recursive ⟶ self-deadlock when same thread tries to lock twice

Operation

Description

Constructor

  • Default only

lock()

Locks, potentially suspending caller if locked by somebody else

unlock()

Unlocks

try_lock()

Does not block; returns true if successful, false otherwise

Use is questionable though!

std::recursive_mutex

  • Same thread may lock one mutex multiple times

  • Must unlock as many times!

Operation

Description

Constructor

  • Default only

lock()

Locks, potentially suspending caller if locked by somebody else

unlock()

Unlocks

try_lock()

Does not block; returns true if successful, false otherwise

Use is questionable though!

std::timed_mutex

  • Like std::mutex, only with try_lock() timeouts

Operation

Description

try_lock_for()

Blocks if unavailable, and returns false if not available some duration into the future

try_lock_until()

Timeout occurs at an absolute time_point in the future

std::recursive_timed_mutex