Typical usage of the last two constructs is demonstrated as follows:
#include <ext/concurrence.h> namespace { __gnu_cxx::__mutex safe_base_mutex; } // anonymous namespace namespace other { void foo() { __gnu_cxx::__scoped_lock sentry(safe_base_mutex); for (int i = 0; i < max; ++i) { _Safe_iterator_base* __old = __iter; __iter = __iter-<_M_next; __old-<_M_detach_single(); } }
In this sample code, an anonymous namespace is used to keep
the __mutex
private to the compilation unit,
and __scoped_lock
is used to guard access to the critical
section within the for loop, locking the mutex on creation and freeing
the mutex as control moves out of this block.
Several exception classes are used to keep track of
concurrence-related errors. These classes
are: __concurrence_lock_error
, __concurrence_unlock_error
, __concurrence_wait_error
,
and __concurrence_broadcast_error
.