[−][src]Struct qt_core::QSemaphoreReleaser
The QSemaphoreReleaser class provides exception-safe deferral of a QSemaphore::release() call.
C++ class: QSemaphoreReleaser
.
The QSemaphoreReleaser class provides exception-safe deferral of a QSemaphore::release() call.
QSemaphoreReleaser can be used wherever you would otherwise use QSemaphore::release(). Constructing a QSemaphoreReleaser defers the release() call on the semaphore until the QSemaphoreReleaser is destroyed (see RAII pattern).
You can use this to reliably release a semaphore to avoid dead-lock in the face of exceptions or early returns:
// ... do something that may throw or return early sem.release();
If an early return is taken or an exception is thrown before the sem.release()
call is reached, the semaphore is not released, possibly preventing the thread waiting in the corresponding sem.acquire()
call from ever continuing execution.
When using RAII instead:
const QSemaphoreReleaser releaser(sem); // ... do something that may throw or early return // implicitly calls sem.release() here and at every other return in between
this can no longer happen, because the compiler will make sure that the QSemaphoreReleaser destructor is always called, and therefore the semaphore is always released.
QSemaphoreReleaser is move-enabled and can therefore be returned from functions to transfer responsibility for releasing a semaphore out of a function or a scope:
{ // some scope QSemaphoreReleaser releaser; // does nothing // ... if (someCondition) { releaser = QSemaphoreReleaser(sem); // ... } // ... } // conditionally calls sem.release(), depending on someCondition
A QSemaphoreReleaser can be canceled by a call to cancel(). A canceled semaphore releaser will no longer call QSemaphore::release() in its destructor.
Methods
impl QSemaphoreReleaser
[src]
pub unsafe fn cancel(&self) -> Ptr<QSemaphore>
[src]
cpp_lib_version="5.11.3"
or cpp_lib_version="5.12.2"
or cpp_lib_version="5.13.0"
or cpp_lib_version="5.14.0"
only.Cancels this QSemaphoreReleaser such that the destructor will no longer call semaphore()->release()
. Returns the value of semaphore() before this call. After this call, semaphore() will return nullptr
.
Calls C++ function: QSemaphore* QSemaphoreReleaser::cancel()
.
Cancels this QSemaphoreReleaser such that the destructor will no longer call semaphore()->release()
. Returns the value of semaphore() before this call. After this call, semaphore() will return nullptr
.
To enable again, assign a new QSemaphoreReleaser:
releaser.cancel(); // avoid releasing old semaphore() releaser = QSemaphoreReleaser(sem, 42); // now will call sem.release(42) when 'releaser' is destroyed
pub unsafe fn new() -> CppBox<QSemaphoreReleaser>
[src]
cpp_lib_version="5.11.3"
or cpp_lib_version="5.12.2"
or cpp_lib_version="5.13.0"
or cpp_lib_version="5.14.0"
only.Default constructor. Creates a QSemaphoreReleaser that does nothing.
Calls C++ function: [constructor] void QSemaphoreReleaser::QSemaphoreReleaser()
.
Default constructor. Creates a QSemaphoreReleaser that does nothing.
pub unsafe fn from_q_semaphore_int(
sem: impl CastInto<Ref<QSemaphore>>,
n: c_int
) -> CppBox<QSemaphoreReleaser>
[src]
sem: impl CastInto<Ref<QSemaphore>>,
n: c_int
) -> CppBox<QSemaphoreReleaser>
cpp_lib_version="5.11.3"
or cpp_lib_version="5.12.2"
or cpp_lib_version="5.13.0"
or cpp_lib_version="5.14.0"
only.Constructor. Stores the arguments and calls sem.release(n) in the destructor.
Calls C++ function: [constructor] void QSemaphoreReleaser::QSemaphoreReleaser(QSemaphore& sem, int n = …)
.
Constructor. Stores the arguments and calls sem.release(n) in the destructor.
pub unsafe fn from_q_semaphore_int2(
sem: impl CastInto<Ptr<QSemaphore>>,
n: c_int
) -> CppBox<QSemaphoreReleaser>
[src]
sem: impl CastInto<Ptr<QSemaphore>>,
n: c_int
) -> CppBox<QSemaphoreReleaser>
cpp_lib_version="5.11.3"
or cpp_lib_version="5.12.2"
or cpp_lib_version="5.13.0"
or cpp_lib_version="5.14.0"
only.Constructor. Stores the arguments and calls sem->release(n) in the destructor.
Calls C++ function: [constructor] void QSemaphoreReleaser::QSemaphoreReleaser(QSemaphore* sem, int n = …)
.
Constructor. Stores the arguments and calls sem->release(n) in the destructor.
pub unsafe fn from_q_semaphore(
sem: impl CastInto<Ref<QSemaphore>>
) -> CppBox<QSemaphoreReleaser>
[src]
sem: impl CastInto<Ref<QSemaphore>>
) -> CppBox<QSemaphoreReleaser>
cpp_lib_version="5.11.3"
or cpp_lib_version="5.12.2"
or cpp_lib_version="5.13.0"
or cpp_lib_version="5.14.0"
only.Constructor. Stores the arguments and calls sem.release(n) in the destructor.
Calls C++ function: [constructor] void QSemaphoreReleaser::QSemaphoreReleaser(QSemaphore& sem)
.
Constructor. Stores the arguments and calls sem.release(n) in the destructor.
pub unsafe fn from_q_semaphore2(
sem: impl CastInto<Ptr<QSemaphore>>
) -> CppBox<QSemaphoreReleaser>
[src]
sem: impl CastInto<Ptr<QSemaphore>>
) -> CppBox<QSemaphoreReleaser>
cpp_lib_version="5.11.3"
or cpp_lib_version="5.12.2"
or cpp_lib_version="5.13.0"
or cpp_lib_version="5.14.0"
only.Constructor. Stores the arguments and calls sem->release(n) in the destructor.
Calls C++ function: [constructor] void QSemaphoreReleaser::QSemaphoreReleaser(QSemaphore* sem)
.
Constructor. Stores the arguments and calls sem->release(n) in the destructor.
pub unsafe fn semaphore(&self) -> Ptr<QSemaphore>
[src]
cpp_lib_version="5.11.3"
or cpp_lib_version="5.12.2"
or cpp_lib_version="5.13.0"
or cpp_lib_version="5.14.0"
only.Returns a pointer to the QSemaphore object provided to the constructor, or by the last move assignment, if any. Otherwise, returns nullptr
.
Calls C++ function: QSemaphore* QSemaphoreReleaser::semaphore() const
.
Returns a pointer to the QSemaphore object provided to the constructor, or by the last move assignment, if any. Otherwise, returns nullptr
.
pub unsafe fn swap(&self, other: impl CastInto<Ref<QSemaphoreReleaser>>)
[src]
cpp_lib_version="5.11.3"
or cpp_lib_version="5.12.2"
or cpp_lib_version="5.13.0"
or cpp_lib_version="5.14.0"
only.Exchanges the responsibilites of *this
and other.
Calls C++ function: void QSemaphoreReleaser::swap(QSemaphoreReleaser& other)
.
Exchanges the responsibilites of *this
and other.
Unlike move assignment, neither of the two objects ever releases its semaphore, if any, as a consequence of swapping.
Therefore this function is very fast and never fails.
Trait Implementations
impl CppDeletable for QSemaphoreReleaser
[src]
unsafe fn delete(&self)
[src]
Unless canceled, calls QSemaphore::release() with the arguments provided to the constructor, or by the last move assignment.
Calls C++ function: [destructor] void QSemaphoreReleaser::~QSemaphoreReleaser()
.
Unless canceled, calls QSemaphore::release() with the arguments provided to the constructor, or by the last move assignment.
Auto Trait Implementations
impl RefUnwindSafe for QSemaphoreReleaser
impl Send for QSemaphoreReleaser
impl Sync for QSemaphoreReleaser
impl Unpin for QSemaphoreReleaser
impl UnwindSafe for QSemaphoreReleaser
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> CastInto<U> for T where
U: CastFrom<T>,
[src]
U: CastFrom<T>,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> StaticUpcast<T> for T
[src]
unsafe fn static_upcast(ptr: Ptr<T>) -> Ptr<T>
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,