| Home | Libraries | People | FAQ | More

## Class read_write_mutex

boost::read_write_mutex —

The read_write_mutex class is a model of the ReadWriteMutex concept.

| Note
Unfortunately it turned out that the current implementation of Read/Write Mutex has some serious problems. So it was decided not to put this implementation into release grade code. Also discussions on the mailing list led to the conclusion that the current concepts need to be rethought. In particular the schedulings Inter-Class Scheduling Policies are deemed unnecessary. There seems to be common belief that a fair scheme suffices. The following documentation has been retained however, to give readers of this document the opportunity to study the original design.

## Synopsis

    class read_write_mutex : private boost::noncopyable,  // Exposition only
                             private boost::noncopyable   // Exposition only
    {
    public:
      // types
      typedef implementation-defined scoped_read_write_lock;
      typedef implementation-defined scoped_read_lock;
      typedef implementation-defined scoped_write_lock;

      // construct/copy/destruct
      read_write_mutex(boost::read_write_scheduling_policy);
      ~read_write_mutex();
    };

## Description

The read_write_mutex class is a model of the ReadWriteMutex concept. It should be used to synchronize access to shared resources using Unspecified locking mechanics.

For classes that model related mutex concepts, see try_read_write_mutex and timed_read_write_mutex.

The read_write_mutex class supplies the following typedefs, which model the specified locking strategies:

Lock Name | Lock Concept
--- | ---
scoped_read_write_lock | ScopedReadWriteLock
scoped_read_lock | ScopedLock
scoped_write_lock | ScopedLock

The read_write_mutex class uses an Unspecified locking strategy, so attempts to recursively lock a read_write_mutex object or attempts to unlock one by threads that don't own a lock on it 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 read/write mutex models in Boost.Thread, read_write_mutex has two types of scheduling policies, an inter-class sheduling policy between threads trying to obtain different types of locks and an intra-class sheduling policy between threads trying to obtain the same type of lock. The read_write_mutex class allows the programmer to choose what inter-class sheduling policy will be used; however, like all read/write mutex models, read_write_mutex leaves the intra-class sheduling policy as Unspecified.

| Note
Self-deadlock is virtually guaranteed if a thread tries to lock the same read_write_mutex multiple times unless all locks are read-locks (but see below)

### `read_write_mutex` construct/copy/destruct

1.     read_write_mutex(boost::read_write_scheduling_policy count);

   Effects: | Constructs a read_write_mutex object.
   Postconditions: | `*this` is in an unlocked state.

2.     ~read_write_mutex();

   Effects: | Destroys a read_write_mutex object.
   Requires: | `*this` is in an unlocked state.
   Notes: | Danger: Destruction of a locked mutex is a serious programming error resulting in undefined behavior such as a program crash.

| Copyright © 2001-2003 William E. Kempf