Online Book Reader

Home Category

Professional C__ - Marc Gregoire [393]

By Root 1466 0
and std::recursive_mutex. Each supports the following methods:

lock(): The calling thread will try to obtain the lock and will block until the lock has been acquired. It will block indefinitely. If there is a desire to limit the amount of time the thread blocks, you should use a time mutex, discussed in the next section.

try_lock(): The calling thread will try to obtain the lock. If the lock is currently held by another thread, the call will return immediately. If the lock has been obtained, try_lock() returns true, otherwise it returns false.

unlock(): Releases the lock held by the calling thread, making it available for another thread.

std::mutex is a standard mutual exclusion class with exclusive ownership semantics. There can be only one thread owning the mutex. If another thread wants to obtain ownership of this mutex, it will either block when using lock(), or fail when using try_lock(). A thread already having ownership of a mutex is not allowed to call lock() or try_lock() again on that mutex. This might lead to a deadlock!

std::recursive_mutex behaves almost identically to std::mutex, except that a thread already having ownership of a recursive mutex is allowed to call lock() or try_lock() again on the same mutex. The calling thread should call the unlock() method as many times as it obtained a lock on the recursive mutex.

Timed Mutex Classes

The library provides a std::timed_mutex and a std::recursive_timed_mutex; both are timed mutex classes supporting the normal lock(), try_lock() and unlock() methods. Additionally they support:

try_lock_for(rel_time): The calling thread will try to obtain the lock for a certain relative time. If the lock could not be obtained after the given timeout, the call fails and returns false. If the lock could be obtained within the timeout, the call succeeds and returns true.

try_lock_until(abs_time): The calling thread will try to obtain the lock until the system time equals or exceeds the specified absolute time. If the lock could be obtained before this time, the call returns true. If the system time passes the given absolute time, the function stops trying to obtain the lock and returns false.

A thread already having ownership of a timed_mutex is not allowed to call one of the previous lock calls again on that mutex. This might lead to a deadlock!

recursive_timed_mutex behaves almost identically to timed_mutex, except that a thread already having ownership of a recursive mutex is allowed to call one of the previous lock calls again on the same mutex. The calling thread should call the unlock() method as many times as it obtained a lock on the recursive mutex.

Locks

A lock class is a wrapper class that makes it easier to obtain and release a lock on a mutex; the destructor of the lock class will automatically release the associated mutex. The standard defines two types of locks: a simple lock, std::lock_guard, whose constructor always acquires the mutex and will block until the lock is acquired; and a more sophisticated std::unique_lock, which allows you to defer lock acquisition until later in the computation, long after the declaration. unique_lock has several constructors:

A constructor accepting a reference to a mutex. This one tries to obtain a lock on the mutex and blocks until the lock is obtained. The keyword explicit for constructors is discussed in Chapter 7.

explicit unique_lock(mutex_type& m);

A constructor accepting a reference to a mutex and an instance of the std::defer_lock_t struct. The unique_lock stores the reference to the mutex, but does not immediately try to obtain a lock. A lock can be obtained later.

unique_lock(mutex_type& m, defer_lock_t) noexcept;

A constructor accepting a reference to a mutex and an instance of the std::try_to_lock_t struct. The lock tries to obtain a lock to the referenced mutex, but if it fails it does not block.

unique_lock(mutex_type& m, try_to_lock_t);

A constructor accepting a reference to a mutex and an instance of the std::adopt_lock_t struct. The lock assumes that the calling thread already

Return Main Page Previous Page Next Page

®Online Book Reader