Subversion Repositories HomeAutomation

Rev

Rev 1317 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1317 Rev 1318
Line 1... Line 1...
1
/*
1
/*
2
 * Queue.h
2
 * Queue.hpp
3
 *
3
 *
4
 *  Created on: Apr 27, 2009
4
 *  Created on: Apr 27, 2009
5
 *      Author: Mattias Runge
5
 *      Author: Mattias Runge
6
 */
6
 */
7
 
7
 
8
#ifndef QUEUE_H_
8
#ifndef QUEUE_HPP_
9
#define QUEUE_H_
9
#define QUEUE_HPP_
10
 
10
 
11
#include <queue>
11
#include <queue>
12
#include <boost/thread.hpp>
12
#include <boost/thread.hpp>
13
#include <boost/thread/mutex.hpp>
13
#include <boost/thread/mutex.hpp>
14
#include <boost/thread/locks.hpp>
14
#include <boost/thread/locks.hpp>
-
 
15
 
-
 
16
/* Note on templates
-
 
17
 * Can not have h/cpp structure because we will get linker problems
-
 
18
 * Read more here: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13
-
 
19
*/
15
 
20
 
16
namespace atom {
21
namespace atom {
17
namespace thread {
22
namespace thread {
18
 
-
 
19
using namespace std;
-
 
20
 
23
 
21
template<typename T>
24
template<typename T>
22
class Queue
25
class Queue
23
{
26
{
24
    typedef boost::mutex::scoped_lock lock;
27
    typedef boost::mutex::scoped_lock lock;
25
 
28
 
26
public:
29
public:
27
    Queue();
30
    Queue()
-
 
31
    {
-
 
32
    }
-
 
33
 
28
    ~Queue();
34
    ~Queue()
-
 
35
    {
-
 
36
    }
-
 
37
 
-
 
38
    void push(T item)
-
 
39
    {
-
 
40
        lock guard(this->myMutex);
-
 
41
        this->myQueue.push(item);
-
 
42
    }
-
 
43
 
-
 
44
    T pop()
-
 
45
    {
-
 
46
        lock guard(this->myMutex);
-
 
47
        T item = this->myQueue.front();
-
 
48
        this->myQueue.pop();
-
 
49
        return item;
-
 
50
    }
29
 
51
 
30
    void push(T item);
52
    unsigned int size()
31
    T pop();
53
    {
-
 
54
        lock guard(this->myMutex);
32
    unsigned int size();
55
        return this->myQueue.size();
-
 
56
    }
33
 
57
 
34
private:
58
private:
35
    std::queue<T> myQueue;
59
    std::queue<T> myQueue;
36
    boost::mutex myMutex;
60
    boost::mutex myMutex;
37
};
61
};