...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
|
Boost.ThreadsHeader <boost/thread/mutex.hpp> |
mutex
mutex
synopsismutex
constructors and
destructortry_mutex
try_mutex
synopsistry_mutex
constructors
and destructortimed_mutex
timed_mutex
synopsistimed_mutex
constructors
and destructorInclude the header <boost/thread/mutex.hpp>
to define the mutex
, try_mutex
and timed_mutex
classes.
The mutex
, try_mutex
and timed_mutex
classes are models of Mutex,
TryMutex, and TimedMutex
respectively. These types should be used to non-recursively synchronize access
to shared resources. For recursive locking mechanics, see the recursive
mutexes supplied by Boost.Threads.
Each class supplies one or more typedefs for lock types which model matching lock concepts. For the best possible performance you should use the mutex class that supports the minimum set of lock types that you need.
Mutex Class | Lock name | Lock Concept |
mutex |
scoped_lock |
ScopedLock |
try_mutex
|
scoped_lock |
ScopedLock ScopedTryLock |
timed_mutex
|
scoped_lock |
ScopedLock ScopedTryLock ScopedTimedLock |
The mutex
, try_mutex
and timed_mutex
classes use an Unspecified
locking
strategy, so attempts to recursively lock them or attempts to unlock them
by threads that don't own a lock on them result in undefined behavior.
This strategy allows implementations to be as efficient as possible on any given
platform. It is, however, recommended that implementations include debugging
support to detect misuse when NDEBUG
is not defined.
Like all the Boost.Threads mutex models,
the mutex
, try_mutex
and timed_mutex
leave the scheduling policy
as Unspecified
. Programmers should make no assumptions about the
order in which waiting threads acquire a lock.
mutex
The mutex
class is a model of Mutex
and NonCopyable, and provides no additional
facilities beyond the requirements of these concepts.
mutex
synopsisnamespace boost { class mutex : private boost::noncopyable // Exposition only. // Class mutex meets the NonCopyable requirement. { public: typedef [implementation defined; see Introduction] scoped_lock; mutex(); ~mutex(); }; };
mutex
constructors and
destructormutex();
*this
is in an unlocked state.~mutex();
*this
is in an unlocked sate.try_mutex
The try_mutex
class is a model of TryMutex
and NonCopyable, and provides no additional
facilities beyond the requirements of these concepts.
try_mutex
synopsisnamespace boost { class try_mutex : private boost::noncopyable // Exposition only. // Class try_mutex meets the NonCopyable requirement. { Public: typedef [implementation defined; see Introduction] scoped_lock; typedef [implementation defined; see Introduction] scoped_try_lock; try_mutex(); ~try_mutex(); }; };
try_mutex
constructors
and destructortry_mutex();
*this
is in an unlocked state.~try_mutex();
*this
is in an unlocked sate.timed_mutex
The timed_mutex
class is a model of TimedMutex
and NonCopyable, and provides no additional
facilities beyond the requirements of these concepts.
timed_mutex
synopsisnamespace boost { class timed_mutex : private boost::noncopyable // Exposition only. // Class timed_mutex meets the NonCopyable requirement. { Public: typedef [implementation defined; see Introduction] scoped_lock; typedef [implementation defined; see Introduction] scoped_try_lock; typedef [implementation defined; see Introduction] scoped_timed_lock; timed_mutex(); ~timed_mutex(); }; };
timed_mutex
constructors
and destructortimed_mutex();
*this
is in an unlocked state.~timed_mutex();
*this
is in an unlocked sate.The output is:
count == 1 count == 2 count == 3 count == 4
Revised 05 November, 2001
© Copyright William E. Kempf 2001-2002. All Rights Reserved.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. William E. Kempf makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.