public member function
<mutex>

std::timed_mutex::lock

void lock();
Lock timed mutex
The calling thread locks the timed_mutex, blocking if necessary (it behaves exactly as in mutex):

  • If the timed_mutex isn't currently locked by any thread, the calling thread locks it (from this point, and until its member unlock is called, the thread owns the timed_mutex).
  • If the timed_mutex is currently locked by another thread, execution of the calling thread is blocked until unlocked by the other thread (other non-locked threads continue their execution).
  • If the timed_mutex is currently locked by the same thread calling this function, it produces a deadlock (with undefined behavior). See recursive_timed_mutex for a timed mutex type that allows multiple locks from the same thread.

All lock and unlock operations on the timed_mutex follow a single total order, with all visible effects synchronized between the lock operations and previous unlock operations on the same object.

Note that the order in which different concurrent locks are scheduled to return is unspecified, and not necessarily related to the order in which they are locked (depending on the system and library implementation).

Parameters

none

Return value

none

Data races

The timed_mutex object is modified as an atomic operation (causing no data races).

Exception safety

Basic guarantee: if an exception is thrown by this member function, the timed_mutex object is left in a valid state. Further, a lock is never acquired by the thread that made the throwing call.
If the timed_mutex is already locked by the current thread, calling this function causes a deadlock (undefined behavior): on certain library implementations, this causes the function to fail.

If the call fails, a system_error exception is thrown:
exception typeerror conditiondescription
system_errorerrc::resource_deadlock_would_occur A deadlock was detected (implementations may detect certain cases of deadlock).
system_errorerrc::operation_not_permitted The thread does not have privileges to perform the operation.
system_errorerrc::device_or_resource_busy The native handle type manipulated is already locked.
Depending on the library implementation, this member function may also throw exceptions to report other situations.

See also