-
Notifications
You must be signed in to change notification settings - Fork 0
/
queues.hpp
37 lines (36 loc) · 1.01 KB
/
queues.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef QUEUES_HPP
#define QUEUES_HPP
#include <queue>
#include <mutex>
#include <memory>
#include "response.hpp"
#include "socket.hpp"
//a thread-safe queue for messages to handle
template <typename T>
class ThreadsafeQueue{
private:
std::queue<T> m_Queue;
public:
std::mutex m_QueueMutex;
void push(T elem){
m_Queue.push(elem);
}
bool pop(T **place){
if(m_Queue.empty()) return false;
if(m_Queue.empty()) return false;
T *ret = new T(m_Queue.front());
m_Queue.pop();
*place = ret;
return true;
}
bool empty(){
return m_Queue.empty();
}
ThreadsafeQueue(): m_Queue(), m_QueueMutex(){
}
};
typedef std::pair<std::string, Socket *> message;//first message string then socket
typedef std::pair<Response *, Socket *> finishedResponse;
typedef ThreadsafeQueue<message> MessageQueue;
typedef ThreadsafeQueue<finishedResponse> ResponseQueue;
#endif