File: std_queue.h

package info (click to toggle)
libcds 2.3.3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,632 kB
  • sloc: cpp: 135,002; ansic: 7,234; perl: 243; sh: 237; makefile: 6
file content (77 lines) | stat: -rw-r--r-- 1,894 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2006-2018 Maxim Khizhinsky
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef CDSUNIT_QUEUE_STD_QUEUE_H
#define CDSUNIT_QUEUE_STD_QUEUE_H

#include <mutex>    //unique_lock
#include <queue>
#include <list>
#include <cds/sync/spinlock.h>

namespace queue {

    template <typename T, class Container, class Lock = cds::sync::spin >
    class StdQueue: public std::queue<T, Container >
    {
        typedef std::queue<T, Container >   base_class;
        mutable Lock m_Locker;

    public:
        bool enqueue( const T& data )
        {
            std::unique_lock<Lock> a(m_Locker);

            base_class::push( data );
            return true;
        }

        bool push( const T& data )
        {
            return enqueue( data );
        }

        bool dequeue( T& data )
        {
            std::unique_lock<Lock> a(m_Locker);
            if ( base_class::empty())
                return false;

            data = base_class::front();
            base_class::pop();
            return true;
        }

        bool pop( T& data )
        {
            return dequeue( data );
        }

        bool empty() const
        {
            std::unique_lock<Lock> a( m_Locker );
            return base_class::empty();
        }

        size_t size() const
        {
            std::unique_lock<Lock> a( m_Locker );
            return base_class::size();
        }

        cds::opt::none statistics() const
        {
            return cds::opt::none();
        }
    };

    template <typename T, class Lock = cds::sync::spin >
    using StdQueue_deque = StdQueue<T, std::deque<T>, Lock >;

    template <typename T, class Lock = cds::sync::spin >
    using StdQueue_list = StdQueue<T, std::list<T>, Lock >;
}

#endif // #ifndef CDSUNIT_QUEUE_STD_QUEUE_H