See Public API for a list of names and headers that are part of the public HPX API.
template<typename Mutex = hpx::spinlock>
- #include <sliding_semaphore.hpp>
A semaphore is a protected variable (an entity storing a value) or abstract data type (an entity grouping several variables that may or may not be numerical) which constitutes the classic method for restricting access to shared resources, such as shared memory, in a multiprogramming environment. Semaphores exist in many variants, though usually the term refers to a counting semaphore, since a binary semaphore is better known as a mutex. A counting semaphore is a counter for a set of available resources, rather than a locked/unlocked flag of a single resource. It was invented by Edsger Dijkstra. Semaphores are the classic solution to preventing race conditions in the dining philosophers problem, although they do not prevent resource deadlocks.
Sliding semaphores can be used for synchronizing multiple threads as well: one thread waiting for several other threads to touch (signal) the semaphore, or several threads waiting for one other thread to touch this semaphore. The difference to a counting semaphore is that a sliding semaphore will not limit the number of threads which are allowed to proceed, but will make sure that the difference between the (arbitrary) number passed to set and wait does not exceed a given threshold.
inline sliding_semaphore_var(std::int64_t max_difference, std::int64_t lower_limit = 0)#
Construct a new sliding semaphore.
inline void set_max_difference(std::int64_t max_difference, std::int64_t lower_limit = 0)#
Set/Change the difference that will cause the semaphore to trigger.
inline void wait(std::int64_t upper_limit)#
Wait for the semaphore to be signaled.
upper_limit – [in] The new upper limit. The calling thread will be suspended if the difference between this value and the largest lower_limit which was set by signal() is larger than the max_difference.
inline bool try_wait(std::int64_t upper_limit = 1)#
Try to wait for the semaphore to be signaled.
The function returns true if the calling thread would not block if it was calling wait().
inline void signal(std::int64_t lower_limit)#
Signal the semaphore.
lower_limit – [in] The new lower limit. This will update the current lower limit of this semaphore. It will also re-schedule all suspended threads for which their associated upper limit is not larger than the lower limit plus the max_difference.
- inline sliding_semaphore_var(std::int64_t max_difference, std::int64_t lower_limit = 0)#
- namespace local
- template<typename Mutex = hpx::spinlock>