read_write_semaphore


An os_read_write_semaphore allows you to ensure that access to an object is limited to a single writer or multiple readers. A read/write semaphore grants a read lock to multiple readers or grants a write lock to a single writer.

Library

Thread<ToolKit>

Declaration

#include <ospace/thread/rwsem.h>

class os_read_write_semaphore

Interface

Constructor
os_read_write_semaphore()
Initializes a read/write with no locks in place.

Throws: os_thread_toolkit_error

read_lock
void read_lock() const
Obtains a read lock. Multiple read locks can exist. If there is an outstanding write lock, this blocks until a read lock can be obtained.

Throws: os_thread_toolkit_error

read_lock_with_yield
void read_lock_with_yield() const
Obtains a read lock. Multiple read locks can exist. If there is an outstanding write lock, this blocks until a read lock can be obtained. If both read and writer compete for a lock at the same time writer gets the priority.

Throws: os_thread_toolkit_error

read_unlock
void read_unlock() const
Releases a read lock held by this thread. It is invalid for a thread to make subsequent calls to read_unlock() without first obtaining a new lock on the mutex.

Throws: os_thread_toolkit_error

try_read_lock
bool try_read_lock() const
Attempts to acquire a read lock. Instead of blocking, this function returns false if it fails to acquire a lock immediately.

Throws: os_thread_toolkit_error

try_read_lock_with_yield
bool try_read_lock_with_yield() const
Attempts to acquire a read lock. Instead of blocking, this function returns false if it fails to acquire a lock immediately.

Throws: os_thread_toolkit_error

try_write_lock
bool try_write_lock()
Attempts to acquire a write lock. Instead of blocking, this function returns false if it fails to acquire a lock immediately.

Throws: os_thread_toolkit_error

write_lock
void write_lock()
Acquires the write lock. This function blocks until a write lock is acquired. Only one thread can own a write lock at a time; however, the same thread can call write_lock repeatedly without blocking on subsequent calls. These write_lock calls must be matched by an equal number of write_unlock calls. A write lock can only exist when there are no read locks.

Throws: os_thread_toolkit_error

write_unlock
void write_unlock()
Releases the write lock held by this thread. It is invalid for a thread to make subsequent calls to write_unlock() without first obtaining a new lock on the mutex.

Throws: os_thread_toolkit_error


Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.