Struct qt_core::QReadWriteLock

source ·
#[repr(C)]
pub struct QReadWriteLock { /* private fields */ }
Expand description

The QReadWriteLock class provides read-write locking.

C++ class: QReadWriteLock.

C++ documentation:

The QReadWriteLock class provides read-write locking.

A read-write lock is a synchronization tool for protecting resources that can be accessed for reading and writing. This type of lock is useful if you want to allow multiple threads to have simultaneous read-only access, but as soon as one thread wants to write to the resource, all other threads must be blocked until the writing is complete.

In many cases, QReadWriteLock is a direct competitor to QMutex. QReadWriteLock is a good choice if there are many concurrent reads and writing occurs infrequently.

Example:

QReadWriteLock lock;

void ReaderThread::run() { ... lock.lockForRead(); read_file(); lock.unlock(); ... }

void WriterThread::run() { ... lock.lockForWrite(); write_file(); lock.unlock(); ... }

To ensure that writers aren't blocked forever by readers, readers attempting to obtain a lock will not succeed if there is a blocked writer waiting for access, even if the lock is currently only accessed by other readers. Also, if the lock is accessed by a writer and another writer comes in, that writer will have priority over any readers that might also be waiting.

Like QMutex, a QReadWriteLock can be recursively locked by the same thread when constructed with QReadWriteLock::Recursive as QReadWriteLock::RecursionMode. In such cases, unlock() must be called the same number of times lockForWrite() or lockForRead() was called. Note that the lock type cannot be changed when trying to lock recursively, i.e. it is not possible to lock for reading in a thread that already has locked for writing (and vice versa).

Implementations§

source§

impl QReadWriteLock

source

pub unsafe fn lock_for_read(&self)

Locks the lock for reading. This function will block the current thread if another thread has locked for writing.

Calls C++ function: void QReadWriteLock::lockForRead().

C++ documentation:

Locks the lock for reading. This function will block the current thread if another thread has locked for writing.

It is not possible to lock for read if the thread already has locked for write.

See also unlock(), lockForWrite(), and tryLockForRead().

source

pub unsafe fn lock_for_write(&self)

Locks the lock for writing. This function will block the current thread if another thread (including the current) has locked for reading or writing (unless the lock has been created using the QReadWriteLock::Recursive mode).

Calls C++ function: void QReadWriteLock::lockForWrite().

C++ documentation:

Locks the lock for writing. This function will block the current thread if another thread (including the current) has locked for reading or writing (unless the lock has been created using the QReadWriteLock::Recursive mode).

It is not possible to lock for write if the thread already has locked for read.

See also unlock(), lockForRead(), and tryLockForWrite().

source

pub unsafe fn new_1a(recursion_mode: RecursionMode) -> CppBox<QReadWriteLock>

Constructs a QReadWriteLock object in the given recursionMode.

Calls C++ function: [constructor] void QReadWriteLock::QReadWriteLock(QReadWriteLock::RecursionMode recursionMode = …).

C++ documentation:

Constructs a QReadWriteLock object in the given recursionMode.

The default recursion mode is NonRecursive.

This function was introduced in Qt 4.4.

See also lockForRead(), lockForWrite(), and RecursionMode.

source

pub unsafe fn new_0a() -> CppBox<QReadWriteLock>

The QReadWriteLock class provides read-write locking.

Calls C++ function: [constructor] void QReadWriteLock::QReadWriteLock().

C++ documentation:

The QReadWriteLock class provides read-write locking.

A read-write lock is a synchronization tool for protecting resources that can be accessed for reading and writing. This type of lock is useful if you want to allow multiple threads to have simultaneous read-only access, but as soon as one thread wants to write to the resource, all other threads must be blocked until the writing is complete.

In many cases, QReadWriteLock is a direct competitor to QMutex. QReadWriteLock is a good choice if there are many concurrent reads and writing occurs infrequently.

Example:

QReadWriteLock lock;

void ReaderThread::run() { ... lock.lockForRead(); read_file(); lock.unlock(); ... }

void WriterThread::run() { ... lock.lockForWrite(); write_file(); lock.unlock(); ... }

To ensure that writers aren't blocked forever by readers, readers attempting to obtain a lock will not succeed if there is a blocked writer waiting for access, even if the lock is currently only accessed by other readers. Also, if the lock is accessed by a writer and another writer comes in, that writer will have priority over any readers that might also be waiting.

Like QMutex, a QReadWriteLock can be recursively locked by the same thread when constructed with QReadWriteLock::Recursive as QReadWriteLock::RecursionMode. In such cases, unlock() must be called the same number of times lockForWrite() or lockForRead() was called. Note that the lock type cannot be changed when trying to lock recursively, i.e. it is not possible to lock for reading in a thread that already has locked for writing (and vice versa).

source

pub unsafe fn try_lock_for_read_0a(&self) -> bool

Attempts to lock for reading. If the lock was obtained, this function returns true, otherwise it returns false instead of waiting for the lock to become available, i.e. it does not block.

Calls C++ function: bool QReadWriteLock::tryLockForRead().

C++ documentation:

Attempts to lock for reading. If the lock was obtained, this function returns true, otherwise it returns false instead of waiting for the lock to become available, i.e. it does not block.

The lock attempt will fail if another thread has locked for writing.

If the lock was obtained, the lock must be unlocked with unlock() before another thread can successfully lock it for writing.

It is not possible to lock for read if the thread already has locked for write.

See also unlock() and lockForRead().

source

pub unsafe fn try_lock_for_read_1a(&self, timeout: c_int) -> bool

This is an overloaded function.

Calls C++ function: bool QReadWriteLock::tryLockForRead(int timeout).

C++ documentation:

This is an overloaded function.

Attempts to lock for reading. This function returns true if the lock was obtained; otherwise it returns false. If another thread has locked for writing, this function will wait for at most timeout milliseconds for the lock to become available.

Note: Passing a negative number as the timeout is equivalent to calling lockForRead(), i.e. this function will wait forever until lock can be locked for reading when timeout is negative.

If the lock was obtained, the lock must be unlocked with unlock() before another thread can successfully lock it for writing.

It is not possible to lock for read if the thread already has locked for write.

See also unlock() and lockForRead().

source

pub unsafe fn try_lock_for_write_0a(&self) -> bool

Attempts to lock for writing. If the lock was obtained, this function returns true; otherwise, it returns false immediately.

Calls C++ function: bool QReadWriteLock::tryLockForWrite().

C++ documentation:

Attempts to lock for writing. If the lock was obtained, this function returns true; otherwise, it returns false immediately.

The lock attempt will fail if another thread has locked for reading or writing.

If the lock was obtained, the lock must be unlocked with unlock() before another thread can successfully lock it.

It is not possible to lock for write if the thread already has locked for read.

See also unlock() and lockForWrite().

source

pub unsafe fn try_lock_for_write_1a(&self, timeout: c_int) -> bool

This is an overloaded function.

Calls C++ function: bool QReadWriteLock::tryLockForWrite(int timeout).

C++ documentation:

This is an overloaded function.

Attempts to lock for writing. This function returns true if the lock was obtained; otherwise it returns false. If another thread has locked for reading or writing, this function will wait for at most timeout milliseconds for the lock to become available.

Note: Passing a negative number as the timeout is equivalent to calling lockForWrite(), i.e. this function will wait forever until lock can be locked for writing when timeout is negative.

If the lock was obtained, the lock must be unlocked with unlock() before another thread can successfully lock it.

It is not possible to lock for write if the thread already has locked for read.

See also unlock() and lockForWrite().

source

pub unsafe fn unlock(&self)

Unlocks the lock.

Calls C++ function: void QReadWriteLock::unlock().

C++ documentation:

Unlocks the lock.

Attempting to unlock a lock that is not locked is an error, and will result in program termination.

See also lockForRead(), lockForWrite(), tryLockForRead(), and tryLockForWrite().

Trait Implementations§

source§

impl CppDeletable for QReadWriteLock

source§

unsafe fn delete(&self)

Destroys the QReadWriteLock object.

Calls C++ function: [destructor] void QReadWriteLock::~QReadWriteLock().

C++ documentation:

Destroys the QReadWriteLock object.

Warning: Destroying a read-write lock that is in use may result in undefined behavior.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T, U> CastInto<U> for T
where U: CastFrom<T>,

source§

unsafe fn cast_into(self) -> U

Performs the conversion. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> StaticUpcast<T> for T

source§

unsafe fn static_upcast(ptr: Ptr<T>) -> Ptr<T>

Convert type of a const pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.