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