locked_queue.hh
author Volker Birk <vb@pep-project.org>
Thu, 16 Apr 2015 20:43:31 +0200
changeset 0 0aa9866d751c
child 269 26c8597fe860
permissions -rw-r--r--
initial commit
     1 #pragma once
     2 
     3 #include <list>
     4 #include <mutex>
     5 
     6 namespace utility
     7 {
     8 	using namespace std;
     9 
    10 	template<class T> class locked_queue
    11 	{
    12 		mutex _mtx;
    13 		list<T> _q;
    14 
    15 	public:
    16 		T& back()
    17 		{
    18 			lock_guard<mutex> lg(_mtx);
    19 			return _q.back();
    20 		}
    21 		T& front()
    22 		{
    23 			lock_guard<mutex> lg(_mtx);
    24 			return _q.front();
    25 		}
    26 		void pop_back()
    27 		{
    28 			lock_guard<mutex> lg(_mtx);
    29 			_q.pop_back();
    30 		}
    31 		void pop_front()
    32 		{
    33 			lock_guard<mutex> lg(_mtx);
    34 			_q.pop_front();
    35 		}
    36 		void push_back(const T& data)
    37 		{
    38 			lock_guard<mutex> lg(_mtx);
    39 			_q.push_back(data);
    40 		}
    41 		void push_front(const T& data)
    42 		{
    43 			lock_guard<mutex> lg(_mtx);
    44 			_q.push_front(data);
    45 		}
    46 		size_t size()
    47 		{
    48 			lock_guard<mutex> lg(_mtx);
    49 			return _q.size();
    50 		}
    51 	};
    52 };