hpx/synchronization/sliding_semaphore.hpp#

Defined in header hpx/synchronization/sliding_semaphore.hpp.

See Public API for a list of names and headers that are part of the public HPX API.

namespace hpx

Typedefs

using sliding_semaphore = sliding_semaphore_var<>#
template<typename Mutex = hpx::spinlock>
class sliding_semaphore_var#
#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.

Public Functions

sliding_semaphore_var(sliding_semaphore_var const&) = delete#
sliding_semaphore_var &operator=(sliding_semaphore_var const&) = delete#
sliding_semaphore_var(sliding_semaphore_var&&) = delete#
sliding_semaphore_var &operator=(sliding_semaphore_var&&) = delete#
inline explicit sliding_semaphore_var(std::int64_t max_difference, std::int64_t lower_limit = 0) noexcept#

Construct a new sliding semaphore.

Parameters
  • max_difference – [in] The max difference between the upper limit (as set by wait()) and the lower limit (as set by signal()) which is allowed without suspending any thread calling wait().

  • lower_limit – [in] The initial lower limit.

inline void set_max_difference(std::int64_t max_difference, std::int64_t lower_limit = 0) noexcept#

Set/Change the difference that will cause the semaphore to trigger

Parameters
  • max_difference – [in] The max difference between the upper limit (as set by wait()) and the lower limit (as set by signal()) which is allowed without suspending any thread calling wait().

  • lower_limit – [in] The initial lower limit.

inline void wait(std::int64_t upper_limit)#

Wait for the semaphore to be signaled.

Parameters

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.

Parameters

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.

Returns

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.

Parameters

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 std::int64_t signal_all()#

Private Types

using mutex_type = Mutex#
using data_type = lcos::local::detail::sliding_semaphore_data<mutex_type>#

Private Members

hpx::intrusive_ptr<data_type> data_#
namespace lcos
namespace local