casacore
Loading...
Searching...
No Matches
casacore::sisco::ConditionalQueue< T > Class Template Reference

A queue with a limited size and the ability to query only specific values. More...

#include <ConditionalQueue.h>

Public Member Functions

 ConditionalQueue (size_t max_size)
 ~ConditionalQueue ()=default
void Push (T &&value, std::unique_lock< std::mutex > &lock)
 Place a new object at the end of the queue.
template<typename... Args>
void Emplace (std::unique_lock< std::mutex > &lock, Args &&... args)
 Same as Push(), but constructs in place.
template<typename Condition>
bool PopIf (T &result, std::unique_lock< std::mutex > &lock, Condition condition)
 Pop an object from the queue for which a specified condition holds.
void NotifyOneChange ()
 Notify the queue that the condition for values might have changed.
void Finish (std::unique_lock< std::mutex > &lock)
 Makes PopIf return false once the queue is empty.
size_t Size (std::unique_lock< std::mutex > &lock) const
size_t MaxSize () const

Private Member Functions

template<typename Condition>
std::list< T >::iterator GetNext (Condition &condition)

Private Attributes

std::list< T > values_
bool is_finished_
size_t max_size_
std::condition_variable pop_condition_
std::condition_variable push_condition_

Detailed Description

template<typename T>
class casacore::sisco::ConditionalQueue< T >

A queue with a limited size and the ability to query only specific values.

The queue is synchronized, but the caller needs to maintain a mutex.

The reason for this is that if the condition requires locking, the same lock can be used for the queue operations and the condition.

Definition at line 20 of file ConditionalQueue.h.

Constructor & Destructor Documentation

◆ ConditionalQueue()

template<typename T>
casacore::sisco::ConditionalQueue< T >::ConditionalQueue ( size_t max_size)
inline

Definition at line 22 of file ConditionalQueue.h.

References max_size_.

◆ ~ConditionalQueue()

template<typename T>
casacore::sisco::ConditionalQueue< T >::~ConditionalQueue ( )
default

Member Function Documentation

◆ Emplace()

template<typename T>
template<typename... Args>
void casacore::sisco::ConditionalQueue< T >::Emplace ( std::unique_lock< std::mutex > & lock,
Args &&... args )
inline

Same as Push(), but constructs in place.

We need to notify all, because if only one thread is awoken that is looking for a specific condition that is not met, the signal is lost while another nother thread's condition may be met.

Definition at line 50 of file ConditionalQueue.h.

References max_size_, pop_condition_, push_condition_, and values_.

◆ Finish()

template<typename T>
void casacore::sisco::ConditionalQueue< T >::Finish ( std::unique_lock< std::mutex > & lock)
inline

Makes PopIf return false once the queue is empty.

Definition at line 104 of file ConditionalQueue.h.

References is_finished_, and push_condition_.

◆ GetNext()

template<typename T>
template<typename Condition>
std::list< T >::iterator casacore::sisco::ConditionalQueue< T >::GetNext ( Condition & condition)
inlineprivate

Definition at line 121 of file ConditionalQueue.h.

References values_.

Referenced by PopIf().

◆ MaxSize()

template<typename T>
size_t casacore::sisco::ConditionalQueue< T >::MaxSize ( ) const
inline

Definition at line 117 of file ConditionalQueue.h.

References max_size_.

◆ NotifyOneChange()

template<typename T>
void casacore::sisco::ConditionalQueue< T >::NotifyOneChange ( )
inline

Notify the queue that the condition for values might have changed.

Definition at line 99 of file ConditionalQueue.h.

References push_condition_.

◆ PopIf()

template<typename T>
template<typename Condition>
bool casacore::sisco::ConditionalQueue< T >::PopIf ( T & result,
std::unique_lock< std::mutex > & lock,
Condition condition )
inline

Pop an object from the queue for which a specified condition holds.

The lock must be owned on entry and will be locked on exit, but may be temporary unlocked during the function.

The condition must be a callable function that takes a value as function parameter and returns a bool. The lock will always be owned when the condition function is called.

If this thread emptied the finished queue while another thread is waiting for a value, it needs to be notified to finish.

Definition at line 72 of file ConditionalQueue.h.

References GetNext(), is_finished_, pop_condition_, push_condition_, and values_.

◆ Push()

template<typename T>
void casacore::sisco::ConditionalQueue< T >::Push ( T && value,
std::unique_lock< std::mutex > & lock )
inline

Place a new object at the end of the queue.

The lock must be owned on entry and will be locked on exit, but may be temporary unlocked during the function.

If the queue is full, the method will block until space is available.

We need to notify all, because if only one thread is awoken that is looking for a specific condition that is not met, the signal is lost while another nother thread's condition may be met.

Definition at line 34 of file ConditionalQueue.h.

References max_size_, casacore::NewDelAllocator< T >::value, pop_condition_, push_condition_, and values_.

◆ Size()

template<typename T>
size_t casacore::sisco::ConditionalQueue< T >::Size ( std::unique_lock< std::mutex > & lock) const
inline

Definition at line 112 of file ConditionalQueue.h.

References values_.

Member Data Documentation

◆ is_finished_

template<typename T>
bool casacore::sisco::ConditionalQueue< T >::is_finished_
private

Definition at line 130 of file ConditionalQueue.h.

Referenced by Finish(), and PopIf().

◆ max_size_

template<typename T>
size_t casacore::sisco::ConditionalQueue< T >::max_size_
private

Definition at line 131 of file ConditionalQueue.h.

Referenced by ConditionalQueue(), Emplace(), MaxSize(), and Push().

◆ pop_condition_

template<typename T>
std::condition_variable casacore::sisco::ConditionalQueue< T >::pop_condition_
private

Definition at line 132 of file ConditionalQueue.h.

Referenced by Emplace(), PopIf(), and Push().

◆ push_condition_

template<typename T>
std::condition_variable casacore::sisco::ConditionalQueue< T >::push_condition_
private

Definition at line 133 of file ConditionalQueue.h.

Referenced by Emplace(), Finish(), NotifyOneChange(), PopIf(), and Push().

◆ values_

template<typename T>
std::list<T> casacore::sisco::ConditionalQueue< T >::values_
private

Definition at line 129 of file ConditionalQueue.h.

Referenced by Emplace(), GetNext(), PopIf(), Push(), and Size().


The documentation for this class was generated from the following file: