test/include/locked_queue.hh
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Mon, 25 Feb 2019 12:28:51 +0100
branchsync_test_refactor
changeset 3314 18a40b81649a
parent 2873 ceaa29b309ef
child 3335 09af2cf0ac74
permissions -rw-r--r--
Ok, doing better, but we still have a memory problem in the single default device case.
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@2853
    26
        T pop_back()
vb@2852
    27
        {
vb@2852
    28
            lock_guard<mutex> lg(_mtx);
vb@2853
    29
            T r = _q.back();
vb@2852
    30
            _q.pop_back();
vb@2853
    31
            return r;
vb@2852
    32
        }
vb@2853
    33
        T pop_front()
vb@2852
    34
        {
vb@2852
    35
            lock_guard<mutex> lg(_mtx);
vb@2853
    36
            T r = _q.front();
vb@2852
    37
            _q.pop_front();
vb@2853
    38
            return r;
vb@2852
    39
        }
vb@2852
    40
        void push_back(const T& data)
vb@2852
    41
        {
vb@2852
    42
            lock_guard<mutex> lg(_mtx);
vb@2852
    43
            _q.push_back(data);
vb@2852
    44
        }
vb@2852
    45
        void push_front(const T& data)
vb@2852
    46
        {
vb@2852
    47
            lock_guard<mutex> lg(_mtx);
vb@2852
    48
            _q.push_front(data);
vb@2852
    49
        }
vb@2852
    50
        size_t size()
vb@2852
    51
        {
vb@2852
    52
            lock_guard<mutex> lg(_mtx);
vb@2852
    53
            return _q.size();
vb@2852
    54
        }
vb@2873
    55
        bool empty()
vb@2873
    56
        {
vb@2873
    57
            lock_guard<mutex> lg(_mtx);
vb@2873
    58
            return _q.empty();
vb@2873
    59
        }
vb@2852
    60
    };
krista@3314
    61
}