Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The TQSemaphore class provides a robust integer semaphore. More...
All the functions in this class are thread-safe when TQt is built with thread support.
#include <ntqsemaphore.h>
A TQSemaphore can be used to serialize thread execution, in a similar way to a TQMutex. A semaphore differs from a mutex, in that a semaphore can be accessed by more than one thread at a time.
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 restuarant. A semaphore is initialized to have a maximum count equal to the number of chairs in the restuarant. 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. Accesses to the sempahore are gained using operator++() or operator+=(), and released with operator--() or 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 actquire accesses without blocking.
See also Environment Classes and Threading.
Warning: If you destroy a semaphore that has accesses in use the resultant behavior is undefined.
Try to get access to the semaphore. If available() == 0, this call will block until it can get access, i.e. until available() > 0.
Release access of the semaphore. This wakes all threads waiting for access to the semaphore.
This file is part of the TQt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.
Copyright © 2007 Trolltech | Trademarks | TQt 3.3.8
|