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.
     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         T pop_back()
    27         {
    28             lock_guard<mutex> lg(_mtx);
    29             T r = _q.back();
    30             _q.pop_back();
    31             return r;
    32         }
    33         T pop_front()
    34         {
    35             lock_guard<mutex> lg(_mtx);
    36             T r = _q.front();
    37             _q.pop_front();
    38             return r;
    39         }
    40         void push_back(const T& data)
    41         {
    42             lock_guard<mutex> lg(_mtx);
    43             _q.push_back(data);
    44         }
    45         void push_front(const T& data)
    46         {
    47             lock_guard<mutex> lg(_mtx);
    48             _q.push_front(data);
    49         }
    50         size_t size()
    51         {
    52             lock_guard<mutex> lg(_mtx);
    53             return _q.size();
    54         }
    55         bool empty()
    56         {
    57             lock_guard<mutex> lg(_mtx);
    58             return _q.empty();
    59         }
    60     };
    61 }