1 #ifndef POSIXPRIORITYSHAREDQUEUE_HPP_
2 #define POSIXPRIORITYSHAREDQUEUE_HPP_
23 template<
typename T,
typename _Priority =
int>
26 std::map< _Priority, std::queue<T> >
queues_;
38 void addQueue(
const _Priority& prio);
40 void push(
const T& data,
const _Priority& prio);
55 template<
typename T,
typename _Priority>
59 if (pthread_mutex_init(&
mutex_, NULL) != 0)
60 throw std::runtime_error(std::string(
"Mutex initialization: ") +
62 if (pthread_cond_init(&
empty_, NULL) != 0)
63 throw std::runtime_error(
64 std::string(
"Condition variable initialization: ") +
71 template<
typename T,
typename _Priority>
84 template<
typename T,
typename _Priority>
88 if (queues_.find(prio) == queues_.end())
89 queues_.insert(std::make_pair<_Priority, std::queue<T> >(
90 prio, std::queue<T>()));
99 template<
typename T,
typename _Priority>
101 const _Priority& prio)
103 pthread_mutex_lock(&mutex_);
104 if (queues_.find(prio) != queues_.end()){
105 queues_[prio].push(data);
107 pthread_mutex_unlock(&mutex_);
108 pthread_cond_signal(&empty_);
111 pthread_mutex_unlock(&mutex_);
120 template<
typename T,
typename _Priority>
125 pthread_cond_wait(&empty_, &mutex_);
126 typename std::map<_Priority, std::queue<T> >::iterator it = queues_.begin();
127 typename std::map<_Priority, std::queue<T> >::iterator itEnd =
129 for(; it != itEnd; ++it){
130 if (!(it->second).empty()){
131 T data = (it->second).front();
150 template<
typename T,
typename _Priority>
154 std::map<_Priority, std::queue<T> > empty;
155 std::swap(queues_, empty);
164 template<
typename T,
typename _Priority>