summaryrefslogtreecommitdiff
path: root/util/queue.h
blob: 710961d7eb8e2102055a7808722f7945e8f43306 (plain)
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
// queue.h

#pragma once

#include "../stdafx.h"
#include "../util/goodies.h"

#include <queue>

namespace mongo {
    
    /**
     * simple blocking queue
     */
    template<typename T> class BlockingQueue : boost::noncopyable {
    public:
        void push(T const& t){
            boostlock l( _lock );
            _queue.push( t );
            _condition.notify_one();
        }
        
        bool empty() const {
            boostlock l( _lock );
            return _queue.empty();
        }
        
        bool tryPop( T & t ){
            boostlock l( _lock );
            if ( _queue.empty() )
                return false;
            
            t = _queue.front();
            _queue.pop();
            
            return true;
        }
        
        T blockingPop(){

            boostlock l( _lock );
            while( _queue.empty() )
                _condition.wait( l );
            
            T t = _queue.front();
            _queue.pop();
            return t;    
        }
        
    private:
        std::queue<T> _queue;
        
        mutable boost::mutex _lock;
        boost::condition _condition;
    };

}