locked_queue.hh
changeset 0 0aa9866d751c
child 269 26c8597fe860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/locked_queue.hh	Thu Apr 16 20:43:31 2015 +0200
     1.3 @@ -0,0 +1,52 @@
     1.4 +#pragma once
     1.5 +
     1.6 +#include <list>
     1.7 +#include <mutex>
     1.8 +
     1.9 +namespace utility
    1.10 +{
    1.11 +	using namespace std;
    1.12 +
    1.13 +	template<class T> class locked_queue
    1.14 +	{
    1.15 +		mutex _mtx;
    1.16 +		list<T> _q;
    1.17 +
    1.18 +	public:
    1.19 +		T& back()
    1.20 +		{
    1.21 +			lock_guard<mutex> lg(_mtx);
    1.22 +			return _q.back();
    1.23 +		}
    1.24 +		T& front()
    1.25 +		{
    1.26 +			lock_guard<mutex> lg(_mtx);
    1.27 +			return _q.front();
    1.28 +		}
    1.29 +		void pop_back()
    1.30 +		{
    1.31 +			lock_guard<mutex> lg(_mtx);
    1.32 +			_q.pop_back();
    1.33 +		}
    1.34 +		void pop_front()
    1.35 +		{
    1.36 +			lock_guard<mutex> lg(_mtx);
    1.37 +			_q.pop_front();
    1.38 +		}
    1.39 +		void push_back(const T& data)
    1.40 +		{
    1.41 +			lock_guard<mutex> lg(_mtx);
    1.42 +			_q.push_back(data);
    1.43 +		}
    1.44 +		void push_front(const T& data)
    1.45 +		{
    1.46 +			lock_guard<mutex> lg(_mtx);
    1.47 +			_q.push_front(data);
    1.48 +		}
    1.49 +		size_t size()
    1.50 +		{
    1.51 +			lock_guard<mutex> lg(_mtx);
    1.52 +			return _q.size();
    1.53 +		}
    1.54 +	};
    1.55 +};