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