Partager via


reader_writer_lock Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

A writer-preference queue-based reader-writer lock with local only spinning. The lock grants first in - first out (FIFO) access to writers and starves readers under a continuous load of writers.

Syntax

class reader_writer_lock;

Members

Public Classes

Name Description
reader_writer_lock::scoped_lock Class An exception safe RAII wrapper that can be used to acquire reader_writer_lock lock objects as a writer.
reader_writer_lock::scoped_lock_read Class An exception safe RAII wrapper that can be used to acquire reader_writer_lock lock objects as a reader.

Public Constructors

Name Description
reader_writer_lock::reader_writer_lock Constructor Constructs a new reader_writer_lock object.
reader_writer_lock::~reader_writer_lock Destructor Destroys the reader_writer_lock object.

Public Methods

Name Description
reader_writer_lock::lock Method Acquires the reader-writer lock as a writer.
reader_writer_lock::lock_read Method Acquires the reader-writer lock as a reader. If there are writers, active readers have to wait until they are done. The reader simply registers an interest in the lock and waits for writers to release it.
reader_writer_lock::try_lock Method Attempts to acquire the reader-writer lock as a writer without blocking.
reader_writer_lock::try_lock_read Method Attempts to acquire the reader-writer lock as a reader without blocking.
reader_writer_lock::unlock Method Unlocks the reader-writer lock based on who locked it, reader or writer.

Remarks

For more information, see Synchronization Data Structures.

Inheritance Hierarchy

reader_writer_lock

Requirements

Header: concrt.h

Namespace: concurrency

reader_writer_lock::lock Method

Acquires the reader-writer lock as a writer.

void lock();

Remarks

It is often safer to utilize the scoped_lock construct to acquire and release a reader_writer_lock object as a writer in an exception safe way.

After a writer attempts to acquire the lock, any future readers will block until the writers have successfully acquired and released the lock. This lock is biased towards writers and can starve readers under a continuous load of writers.

Writers are chained so that a writer exiting the lock releases the next writer in line.

If the lock is already held by the calling context, an improper_lock exception will be thrown.

reader_writer_lock::lock_read Method

Acquires the reader-writer lock as a reader. If there are writers, active readers have to wait until they are done. The reader simply registers an interest in the lock and waits for writers to release it.

void lock_read();

Remarks

It is often safer to utilize the scoped_lock_read construct to acquire and release a reader_writer_lock object as a reader in an exception safe way.

If there are writers waiting on the lock, the reader will wait until all writers in line have acquired and released the lock. This lock is biased towards writers and can starve readers under a continuous load of writers.

reader_writer_lock::reader_writer_lock Constructor

Constructs a new reader_writer_lock object.

reader_writer_lock();

reader_writer_lock::~reader_writer_lock Destructor

Destroys the reader_writer_lock object.

~reader_writer_lock();

Remarks

It is expected that the lock is no longer held when the destructor runs. Allowing the reader writer lock to destruct with the lock still held results in undefined behavior.

reader_writer_lock::scoped_lock Class

An exception safe RAII wrapper that can be used to acquire reader_writer_lock lock objects as a writer.

class scoped_lock;

reader_writer_lock::scoped_lock::scoped_lock Constructor

Constructs a scoped_lock object and acquires the reader_writer_lock object passed in the _Reader_writer_lock parameter as a writer. If the lock is held by another thread, this call will block.

explicit _CRTIMP scoped_lock(reader_writer_lock& _Reader_writer_lock);

Parameters

_Reader_writer_lock
The reader_writer_lock object to acquire as a writer.

reader_writer_lock::scoped_lock::~scoped_lock Destructor

Destroys a reader_writer_lock object and releases the lock supplied in its constructor.

~scoped_lock();

reader_writer_lock::scoped_lock_read Class

An exception safe RAII wrapper that can be used to acquire reader_writer_lock lock objects as a reader.

class scoped_lock_read;

reader_writer_lock::try_lock Method

Attempts to acquire the reader-writer lock as a writer without blocking.

reader_writer_lock::scoped_lock_read::scoped_lock_read Constructor

Constructs a scoped_lock_read object and acquires the reader_writer_lock object passed in the _Reader_writer_lock parameter as a reader. If the lock is held by another thread as a writer or there are pending writers, this call will block.

explicit _CRTIMP scoped_lock_read(reader_writer_lock& _Reader_writer_lock);

Parameters

_Reader_writer_lock
The reader_writer_lock object to acquire as a reader.

reader_writer_lock::scoped_lock_read::~scoped_lock_read Destructor

Destroys a scoped_lock_read object and releases the lock supplied in its constructor.

~scoped_lock_read();

reader_writer_lock::try_lock Method

bool try_lock();

Return Value

If the lock was acquired, the value true; otherwise, the value false.

reader_writer_lock::try_lock_read Method

Attempts to acquire the reader-writer lock as a reader without blocking.

bool try_lock_read();

Return Value

If the lock was acquired, the value true; otherwise, the value false.

reader_writer_lock::unlock Method

Unlocks the reader-writer lock based on who locked it, reader or writer.

void unlock();

Remarks

If there are writers waiting on the lock, the release of the lock will always go to the next writer in FIFO order. This lock is biased towards writers and can starve readers under a continuous load of writers.

See Also

concurrency Namespace
critical_section Class