- class QSemaphore#
The
QSemaphore
class provides a general counting semaphore. More…Synopsis#
Methods#
def
__init__()
def
acquire()
def
available()
def
release()
def
tryAcquire()
def
try_acquire()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description#
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
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()
andrelease()
: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 anavailable()
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 Producer and Consumer using 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, theavailable()
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).- __init__([n=0])#
- Parameters:
n – int
Creates a new semaphore and initializes the number of resources it guards to
n
(by default, 0).See also
- acquire([n=1])#
- Parameters:
n – int
Tries to acquire
n
resources guarded by the semaphore. Ifn
>available()
, this call will block until enough resources are available.See also
- available()#
- Return type:
int
Returns the number of resources currently available to the semaphore. This number can never be negative.
- release([n=1])#
- Parameters:
n – int
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
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 resources()
QSemaphoreReleaser
is a RAII wrapper around this function.See also
- tryAcquire([n=1])#
- Parameters:
n – int
- Return type:
bool
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Tries to acquire
n
resources guarded by the semaphore and returnstrue
on success. Ifavailable()
<n
, this call immediately returnsfalse
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
- tryAcquire(n, timeout)
- Parameters:
n – int
timeout –
QDeadlineTimer
- Return type:
bool
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Tries to acquire
n
resources guarded by the semaphore and returnstrue
on success. Ifavailable()
<n
, this call will wait untiltimer
expires for resources to become available.Example:
QSemaphore sem(5) # sem.available() == 5 sem.tryAcquire(250, QDeadlineTimer(1000)) # sem.available() == 5, waits 1000 milliseconds and returns false sem.tryAcquire(3, QDeadlineTimer(30s)) # sem.available() == 2, returns true without waiting
See also
- tryAcquire(n, timeout)
- Parameters:
n – int
timeout – int
- Return type:
bool
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Tries to acquire
n
resources guarded by the semaphore and returnstrue
on success. Ifavailable()
<n
, this call will wait for at mosttimeout
milliseconds for resources to become available.Note: Passing a negative number as the
timeout
is equivalent to callingacquire()
, i.e. this function will wait forever for resources to become available iftimeout
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
- try_acquire()#
- Return type:
bool
This function is provided for
std::counting_semaphore
compatibility.It is equivalent to calling
tryAcquire(1)
, where the function returnstrue
on acquiring the resource successfully.See also
tryAcquire()
try_acquire_for()
try_acquire_until()