PythonQt.QtCore.QSystemSemaphore
QSystemSemaphore
PythonQtInstanceWrapper object
Native bases: PythonQtInstanceWrapper
Supplemental documentation: Qt 5 reference:
Detailed Description
A semaphore is a generalization of a mutex. While a mutex can be locked only once, a semaphore can be acquired multiple times. Typically, a semaphore is used to protect a certain number of identical resources.
Like its lighter counterpart QSemaphore, a QSystemSemaphore can be accessed from multiple threads. Unlike QSemaphore, a QSystemSemaphore can also be accessed from multiple processes. This means QSystemSemaphore is a much heavier class, so if your application doesn't need to access your semaphores across multiple processes, you will probably want to use QSemaphore.
Semaphores support two fundamental operations, acquire() and release():
acquire() tries to acquire one resource. If there isn't a resource available, the call blocks until a resource becomes available. Then the resource is acquired and the call returns.
release() releases one resource so it can be acquired by another process. The function can also be called with a parameter n > 1, which releases n resources.
A system semaphore is created with a string key that other processes can use to use the same semaphore.
Example: Create a system semaphore
QSystemSemaphore sem("market", 3, QSystemSemaphore::Create);
// resources available == 3
sem.acquire(); // resources available == 2
sem.acquire(); // resources available == 1
sem.acquire(); // resources available == 0
sem.release(); // resources available == 1
sem.release(2); // resources available == 3
A typical application of system semaphores is for controlling access to a circular buffer shared by a producer process and a consumer processes.
Platform-Specific Behavior
When using this class, be aware of the following platform differences:
Windows: QSystemSemaphore does not own its underlying system semaphore. Windows owns it. This means that when all instances of QSystemSemaphore for a particular key have been destroyed, either by having their destructors called, or because one or more processes crash, Windows removes the underlying system semaphore.
Unix:
- QSystemSemaphore owns the underlying system semaphore in Unix systems. This means that the last process having an instance of QSystemSemaphore for a particular key must remove the underlying system semaphore in its destructor. If the last process crashes without running the QSystemSemaphore destructor, Unix does not automatically remove the underlying system semaphore, and the semaphore survives the crash. A subsequent process that constructs a QSystemSemaphore with the same key will then be given the existing system semaphore. In that case, if the QSystemSemaphore constructor has specified its access mode as Open, its initial resource count will not be reset to the one provided but remain set to the value it received in the crashed process. To protect against this, the first process to create a semaphore for a particular key (usually a server), must pass its access mode as Create, which will force Unix to reset the resource count in the underlying system semaphore.
- When a process using QSystemSemaphore terminates for any reason, Unix automatically reverses the effect of all acquire operations that were not released. Thus if the process acquires a resource and then exits without releasing it, Unix will release that resource.
Source: https://doc.qt.io/archives/qt-5.15/qsystemsemaphore.html
Complete member index
Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.
| Member | Kind | Signature | Summary |
|---|---|---|---|
AccessMode |
attribute | type |
Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text) |
AlreadyExists |
attribute | Any |
(shared captured text) |
Create |
attribute | Any |
(shared captured text) |
KeyError |
attribute | Any |
(shared captured text) |
NoError |
attribute | Any |
(shared captured text) |
NotFound |
attribute | Any |
(shared captured text) |
Open |
attribute | Any |
|
OutOfResources |
attribute | Any |
(shared captured text) |
PermissionDenied |
attribute | Any |
(shared captured text) |
SystemSemaphoreError |
attribute | type |
Convert a number or string to an integer, or return 0 if no arguments are given. |
UnknownError |
attribute | Any |
(shared captured text) |
__init__ |
function | (self, *args: Any, **kwargs: Any) -> None |
Native constructor; consult class documentation for supported overloads. (shared captured text) |
acquire |
function | (self) -> bool |
X.acquire() -> bool |
className |
function | (self, *args: Any, **kwargs: Any) -> Any |
Return the classname of the object (shared captured text) |
delete |
function | (self, *args: Any, **kwargs: Any) -> Any |
Deletes the C++ object (at your own risk, my friend!) (shared captured text) |
error |
function | (self) -> Any |
Returns a value indicating whether an error occurred, and, if so, which error it was. |
errorString |
function | (self) -> str |
X.errorString() -> str |
help |
function | (self, *args: Any, **kwargs: Any) -> Any |
Shows the help of available methods for this class (shared captured text) |
inherits |
function | (self, *args: Any, **kwargs: Any) -> Any |
Returns if the class inherits or is of given type name (shared captured text) |
key |
function | (self) -> str |
X.key() -> str |
release |
function | (self, n: int) -> bool |
X.release(n) -> bool |
setKey |
function | (self, key: str, initialValue: int, mode: Any) -> None |
X.setKey(key, initialValue, mode) |