# reader_writer_lock Class

## Summary

A PPL-compatible reader-writer mutex that is scalable and gives preference to writers.

## Syntax

    class reader_writer_lock;

## Header

    #include "tbb/reader_writer_lock.h"

## Description

A `reader_writer_lock` implements a PPL-compatible reader-writer mutex. A `reader_writer_lock` is scalable and nonrecursive. The implementation handles lock requests on a first-come first-serve basis except that writers have preference over readers. Waiting threads busy wait, which can degrade system performance if the wait is long. However, if the wait is typically short, a `reader_writer_lock` can provide performance competitive with other mutexes.

A`reader_writer_lock` models part of the ReaderWriterMutex Concept and part of the C++11 compatibility interface. The major differences are:

- The scoped interfaces support only strictly scoped locks. For example, the method `scoped_lock::release()` is not supported.

- Reader locking has a separate interface. For example, there is separate scoped interface scoped_lock_read for reader locking, instead of a flag to distinguish the reader cases as in the ReaderWriterMutex Concept.

## Members

    namespace tbb {
        class reader_writer_lock {
        public:
            reader_writer_lock();
            ~reader_writer_lock();
            void lock();
            void lock_read();
            bool try_lock();
            bool try_lock_read();
            void unlock();
             class scoped_lock {
            public:
                scoped_lock( reader_writer_lock& mutex );
                ~scoped_lock();
            };
            class scoped_lock_read {
            public:
                scoped_lock_read( reader_writer_lock& mutex );
                ~scoped_lock_read();
            };
       };}

The following table summarizes the semantics.

reader_writer_lock Members Summary
Member | Semantics
--- | ---
`reader_writer_lock()` | Construct unlocked mutex.
`~reader_writer_lock()` | Destroy unlocked mutex.
`void reader_writer_lock::lock()` | Acquire write lock on mutex.
`void reader_writer_lock::lock_read()` | Acquire read lock on mutex.
`bool reader_writer_lock::try_lock()` | Try to acquire write lock on mutex. Returns true if lock acquired, false otherwise.
`bool reader_writer_lock::try_lock_read()` | Try to acquire read lock on mutex. Returns true if lock acquired, false otherwise.
`reader_writer_lock::unlock()` | Release lock.
`reader_writer_lock::scoped_lock (reader_writer_lock& m)` | Acquire write lock on mutex m.
`reader_writer_lock::~scoped_lock()` | Release write lock (if acquired).
`reader_writer_lock::scoped_lock_read (reader_writer_lock& m)` | Acquire read lock on mutex m.
`reader_writer_lock::~scoped_lock_read()` | Release read lock (if acquired).

Parent topic: PPL Compatibility

## See Also

Mutext Concept
ReaderWriterMutex Concept