Horizon Official Technical Documentation
ThreadSafeQueue< T > Class Template Reference

#include <ThreadSafeQueue.hpp>

+ Collaboration diagram for ThreadSafeQueue< T >:

Classes

struct  node
 

Public Member Functions

 ThreadSafeQueue ()
 
 ~ThreadSafeQueue ()
 
 ThreadSafeQueue (const ThreadSafeQueue &other)=delete
 
ThreadSafeQueueoperator= (const ThreadSafeQueue &other)=delete
 
std::shared_ptr< T > try_pop ()
 
void push (T &&new_value)
 
std::size_t size ()
 
bool empty ()
 
std::shared_ptr< T > front ()
 

Private Member Functions

nodeget_tail ()
 
std::unique_ptr< nodepop_head ()
 

Private Attributes

std::mutex head_mutex
 
std::unique_ptr< nodehead
 
std::mutex tail_mutex
 
nodetail
 

Constructor & Destructor Documentation

◆ ThreadSafeQueue() [1/2]

template<typename T >
ThreadSafeQueue< T >::ThreadSafeQueue ( )
inline
69 :
70 head(new node), tail(head.get())
71 {}
node * tail
Definition: ThreadSafeQueue.hpp:48
std::unique_ptr< node > head
Definition: ThreadSafeQueue.hpp:46

◆ ~ThreadSafeQueue()

template<typename T >
ThreadSafeQueue< T >::~ThreadSafeQueue ( )
inline
74 {
75 std::size_t qsize = size();
76
77 for (std::size_t i = 0; i < qsize; i++)
78 try_pop();
79 }
std::size_t size()
Definition: ThreadSafeQueue.hpp:102
std::shared_ptr< T > try_pop()
Definition: ThreadSafeQueue.hpp:84

References ThreadSafeQueue< T >::size(), and ThreadSafeQueue< T >::try_pop().

+ Here is the call graph for this function:

◆ ThreadSafeQueue() [2/2]

template<typename T >
ThreadSafeQueue< T >::ThreadSafeQueue ( const ThreadSafeQueue< T > &  other)
delete

Member Function Documentation

◆ empty()

template<typename T >
bool ThreadSafeQueue< T >::empty ( )
inline
117 {
118 std::unique_lock<std::mutex> head_lock(head_mutex);
119
120 if(head.get() == get_tail())
121 return true;
122
123 return false;
124 }
node * get_tail()
Definition: ThreadSafeQueue.hpp:50
std::mutex head_mutex
Definition: ThreadSafeQueue.hpp:45

References ThreadSafeQueue< T >::get_tail(), ThreadSafeQueue< T >::head, and ThreadSafeQueue< T >::head_mutex.

Referenced by Horizon::Networking::Socket< SocketType >::handle_queue(), and Horizon::Networking::Socket< SocketType >::update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ front()

template<typename T >
std::shared_ptr< T > ThreadSafeQueue< T >::front ( )
inline
127 {
128 std::unique_lock<std::mutex> head_lock(head_mutex);
129 std::shared_ptr<T> front = head != nullptr ? std::make_shared<T>(*head->data) : nullptr;
130 return std::move(front);
131 }
std::shared_ptr< T > front()
Definition: ThreadSafeQueue.hpp:126

References ThreadSafeQueue< T >::front(), ThreadSafeQueue< T >::head, and ThreadSafeQueue< T >::head_mutex.

Referenced by ThreadSafeQueue< T >::front(), and Horizon::Networking::Socket< SocketType >::handle_queue().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_tail()

template<typename T >
node * ThreadSafeQueue< T >::get_tail ( )
inlineprivate
51 {
52 std::unique_lock<std::mutex> tail_lock(tail_mutex);
53 return tail;
54 }
std::mutex tail_mutex
Definition: ThreadSafeQueue.hpp:47

References ThreadSafeQueue< T >::tail, and ThreadSafeQueue< T >::tail_mutex.

Referenced by ThreadSafeQueue< T >::empty(), and ThreadSafeQueue< T >::pop_head().

+ Here is the caller graph for this function:

◆ operator=()

template<typename T >
ThreadSafeQueue & ThreadSafeQueue< T >::operator= ( const ThreadSafeQueue< T > &  other)
delete

◆ pop_head()

template<typename T >
std::unique_ptr< node > ThreadSafeQueue< T >::pop_head ( )
inlineprivate
57 {
58 std::unique_lock<std::mutex> head_lock(head_mutex);
59
60 if(head.get() == get_tail())
61 return nullptr;
62
63 std::unique_ptr<node> old_head = std::move(head);
64 head = std::move(old_head->next);
65 return old_head;
66 }

References ThreadSafeQueue< T >::get_tail(), ThreadSafeQueue< T >::head, and ThreadSafeQueue< T >::head_mutex.

Referenced by ThreadSafeQueue< T >::try_pop().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ push()

template<typename T >
void ThreadSafeQueue< T >::push ( T &&  new_value)
inline
91 {
92 std::shared_ptr<T> new_data(
93 std::make_shared<T>(std::move(new_value)));
94 std::unique_ptr<node> p(new node);
95 node *new_tail = p.get();
96 std::unique_lock<std::mutex> tail_lock(tail_mutex);
97 tail->data = new_data;
98 tail->next = std::move(p);
99 tail = new_tail;
100 }
std::shared_ptr< T > data
Definition: ThreadSafeQueue.hpp:41
std::unique_ptr< node > next
Definition: ThreadSafeQueue.hpp:42

References ThreadSafeQueue< T >::node::data, ThreadSafeQueue< T >::node::next, ThreadSafeQueue< T >::tail, and ThreadSafeQueue< T >::tail_mutex.

Referenced by BOOST_AUTO_TEST_CASE(), Horizon::Networking::Socket< SocketType >::queue_buffer(), Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::set_socket_for_management(), Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::set_socket_for_removal(), and WorkerThreadPool::submit().

+ Here is the caller graph for this function:

◆ size()

template<typename T >
std::size_t ThreadSafeQueue< T >::size ( )
inline
103 {
104 int count = 0;
105 std::unique_lock<std::mutex> head_lock(head_mutex);
106 std::unique_lock<std::mutex> tail_lock(tail_mutex);
107 node const *n = head.get();
108
109 while ((n = n->next.get())) {
110 count++;
111 }
112
113 return count;
114 }
size_t count(GridTypeListContainer< SPECIFIC_TYPE > const &elements, SPECIFIC_TYPE *)
Definition: GridReferenceContainer.hpp:100

References GridTypeListIterator::count(), ThreadSafeQueue< T >::head, ThreadSafeQueue< T >::head_mutex, ThreadSafeQueue< T >::node::next, and ThreadSafeQueue< T >::tail_mutex.

Referenced by BOOST_AUTO_TEST_CASE(), and ThreadSafeQueue< T >::~ThreadSafeQueue().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ try_pop()

template<typename T >
std::shared_ptr< T > ThreadSafeQueue< T >::try_pop ( )
inline
85 {
86 std::unique_ptr<node> old_head = pop_head();
87 return old_head ? old_head->data : std::shared_ptr<T>();
88 }
std::unique_ptr< node > pop_head()
Definition: ThreadSafeQueue.hpp:56

References ThreadSafeQueue< T >::pop_head().

Referenced by BOOST_AUTO_TEST_CASE(), Horizon::Networking::Socket< SocketType >::handle_queue(), Horizon::Networking::AcceptSocketMgr< SocketType, NetworkThreadType >::manage_sockets(), WorkerThreadPool::worker_thread(), and ThreadSafeQueue< T >::~ThreadSafeQueue().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ head

◆ head_mutex

◆ tail

template<typename T >
node* ThreadSafeQueue< T >::tail
private

◆ tail_mutex

template<typename T >
std::mutex ThreadSafeQueue< T >::tail_mutex
private

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