Struct qt_core::QSemaphore

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

The QSemaphore class provides a general counting semaphore.

C++ class: QSemaphore.

C++ documentation:

The QSemaphore class provides a general counting semaphore.

A semaphore is a generalization of a mutex. While a mutex can only be locked once, it's possible to acquire a semaphore multiple times. Semaphores are typically used to protect a certain number of identical resources.

Semaphores support two fundamental operations, acquire() and release():

  • acquire(n) tries to acquire n resources. If there aren't that many resources available, the call will block until this is the case.
  • release(n) releases n resources.

There's also a tryAcquire() function that returns immediately if it cannot acquire the resources, and an available() function that returns the number of available resources at any time.

Example:

QSemaphore sem(5); // sem.available() == 5

sem.acquire(3); // sem.available() == 2 sem.acquire(2); // sem.available() == 0 sem.release(5); // sem.available() == 5 sem.release(5); // sem.available() == 10

sem.tryAcquire(1); // sem.available() == 9, returns true sem.tryAcquire(250); // sem.available() == 9, returns false

A typical application of semaphores is for controlling access to a circular buffer shared by a producer thread and a consumer thread. The Semaphores Example shows how to use QSemaphore to solve that problem.

A non-computing example of a semaphore would be dining at a restaurant. A semaphore is initialized with the number of chairs in the restaurant. As people arrive, they want a seat. As seats are filled, available() is decremented. As people leave, the available() is incremented, allowing more people to enter. If a party of 10 people want to be seated, but there are only 9 seats, those 10 people will wait, but a party of 4 people would be seated (taking the available seats to 5, making the party of 10 people wait longer).

Implementations§

source§

impl QSemaphore

source

pub unsafe fn acquire_1a(&self, n: c_int)

Tries to acquire n resources guarded by the semaphore. If n > available(), this call will block until enough resources are available.

Calls C++ function: void QSemaphore::acquire(int n = …).

C++ documentation:

Tries to acquire n resources guarded by the semaphore. If n > available(), this call will block until enough resources are available.

See also release(), available(), and tryAcquire().

source

pub unsafe fn acquire_0a(&self)

Tries to acquire n resources guarded by the semaphore. If n > available(), this call will block until enough resources are available.

Calls C++ function: void QSemaphore::acquire().

C++ documentation:

Tries to acquire n resources guarded by the semaphore. If n > available(), this call will block until enough resources are available.

See also release(), available(), and tryAcquire().

source

pub unsafe fn available(&self) -> c_int

Returns the number of resources currently available to the semaphore. This number can never be negative.

Calls C++ function: int QSemaphore::available() const.

C++ documentation:

Returns the number of resources currently available to the semaphore. This number can never be negative.

See also acquire() and release().

source

pub unsafe fn new_1a(n: c_int) -> CppBox<QSemaphore>

Creates a new semaphore and initializes the number of resources it guards to n (by default, 0).

Calls C++ function: [constructor] void QSemaphore::QSemaphore(int n = …).

C++ documentation:

Creates a new semaphore and initializes the number of resources it guards to n (by default, 0).

See also release() and available().

source

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

The QSemaphore class provides a general counting semaphore.

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

C++ documentation:

The QSemaphore class provides a general counting semaphore.

A semaphore is a generalization of a mutex. While a mutex can only be locked once, it's possible to acquire a semaphore multiple times. Semaphores are typically used to protect a certain number of identical resources.

Semaphores support two fundamental operations, acquire() and release():

  • acquire(n) tries to acquire n resources. If there aren't that many resources available, the call will block until this is the case.
  • release(n) releases n resources.

There's also a tryAcquire() function that returns immediately if it cannot acquire the resources, and an available() function that returns the number of available resources at any time.

Example:

QSemaphore sem(5); // sem.available() == 5

sem.acquire(3); // sem.available() == 2 sem.acquire(2); // sem.available() == 0 sem.release(5); // sem.available() == 5 sem.release(5); // sem.available() == 10

sem.tryAcquire(1); // sem.available() == 9, returns true sem.tryAcquire(250); // sem.available() == 9, returns false

A typical application of semaphores is for controlling access to a circular buffer shared by a producer thread and a consumer thread. The Semaphores Example shows how to use QSemaphore to solve that problem.

A non-computing example of a semaphore would be dining at a restaurant. A semaphore is initialized with the number of chairs in the restaurant. As people arrive, they want a seat. As seats are filled, available() is decremented. As people leave, the available() is incremented, allowing more people to enter. If a party of 10 people want to be seated, but there are only 9 seats, those 10 people will wait, but a party of 4 people would be seated (taking the available seats to 5, making the party of 10 people wait longer).

source

pub unsafe fn release_1a(&self, n: c_int)

Releases n resources guarded by the semaphore.

Calls C++ function: void QSemaphore::release(int n = …).

C++ documentation:

Releases n resources guarded by the semaphore.

This function can be used to "create" resources as well. For example:

QSemaphore sem(5); // a semaphore that guards 5 resources sem.acquire(5); // acquire all 5 resources sem.release(5); // release the 5 resources sem.release(10); // “create” 10 new resources

See also acquire() and available().

source

pub unsafe fn release_0a(&self)

Releases n resources guarded by the semaphore.

Calls C++ function: void QSemaphore::release().

C++ documentation:

Releases n resources guarded by the semaphore.

This function can be used to "create" resources as well. For example:

QSemaphore sem(5); // a semaphore that guards 5 resources sem.acquire(5); // acquire all 5 resources sem.release(5); // release the 5 resources sem.release(10); // “create” 10 new resources

See also acquire() and available().

source

pub unsafe fn try_acquire_1a(&self, n: c_int) -> bool

Tries to acquire n resources guarded by the semaphore and returns true on success. If available() < n, this call immediately returns false without acquiring any resources.

Calls C++ function: bool QSemaphore::tryAcquire(int n = …).

C++ documentation:

Tries to acquire n resources guarded by the semaphore and returns true on success. If available() < n, this call immediately returns false without acquiring any resources.

Example:

QSemaphore sem(5); // sem.available() == 5 sem.tryAcquire(250); // sem.available() == 5, returns false sem.tryAcquire(3); // sem.available() == 2, returns true

See also acquire().

source

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

Tries to acquire n resources guarded by the semaphore and returns true on success. If available() < n, this call will wait for at most timeout milliseconds for resources to become available.

Calls C++ function: bool QSemaphore::tryAcquire(int n, int timeout).

C++ documentation:

Tries to acquire n resources guarded by the semaphore and returns true on success. If available() < n, this call will wait for at most timeout milliseconds for resources to become available.

Note: Passing a negative number as the timeout is equivalent to calling acquire(), i.e. this function will wait forever for resources to become available if timeout is negative.

Example:

QSemaphore sem(5); // sem.available() == 5 sem.tryAcquire(250, 1000); // sem.available() == 5, waits 1000 milliseconds and returns false sem.tryAcquire(3, 30000); // sem.available() == 2, returns true without waiting

See also acquire().

source

pub unsafe fn try_acquire_0a(&self) -> bool

Tries to acquire n resources guarded by the semaphore and returns true on success. If available() < n, this call immediately returns false without acquiring any resources.

Calls C++ function: bool QSemaphore::tryAcquire().

C++ documentation:

Tries to acquire n resources guarded by the semaphore and returns true on success. If available() < n, this call immediately returns false without acquiring any resources.

Example:

QSemaphore sem(5); // sem.available() == 5 sem.tryAcquire(250); // sem.available() == 5, returns false sem.tryAcquire(3); // sem.available() == 2, returns true

See also acquire().

Trait Implementations§

source§

impl CppDeletable for QSemaphore

source§

unsafe fn delete(&self)

Destroys the semaphore.

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

C++ documentation:

Destroys the semaphore.

Warning: Destroying a semaphore 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.