Note: All functions in this class are thread-safe.
The Q3Semaphore class provides a robust integer semaphore.
For example, suppose we have an application that stores data in a large tree structure. The application creates 10 threads (commonly called a thread pool) to perform searches on the tree. When the application searches the tree for some piece of data, it uses one thread per base node to do the searching. A semaphore could be used to make sure that two threads don't try to search the same branch of the tree at the same time.
A non-computing example of a semaphore would be dining at a restaurant. A semaphore is initialized to have a maximum count equal to the number of chairs in the restaurant. As people arrive, they want a seat. As seats are filled, the semaphore is accessed, once per person. As people leave, the access is released, 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).
When a semaphore is created it is given a number which is the maximum number of concurrent accesses it will permit. This amount may be changed using operator++(), operator--(), operator+=() and operator-=(). The number of accesses allowed is retrieved with available(), and the total number with total(). Note that the incrementing functions will block if there aren't enough available accesses. Use tryAccess() if you want to acquire accesses without blocking.
Member Function Documentation
Creates a new semaphore. The semaphore can be concurrently accessed at most maxcount times.
Destroys the semaphore.
Warning: If you destroy a semaphore that has accesses in use the resultant behavior is undefined.
Returns the number of accesses currently available to the semaphore.
Returns the total number of accesses to the semaphore.
Try to get access to the semaphore. If available() < n, this function will return false immediately. If available() >= n, this function will take n accesses and return true. This function does not block.
Postfix ++ operator.
Postfix -- operator.
Release access of the semaphore. This wakes all threads waiting for access to the semaphore.
Release n accesses to the semaphore.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.