KISSCPP
a C++ library for rapid application development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
threadsafe_persisted_queue.hpp
Go to the documentation of this file.
1 // File : threadsafe_persisted_queue.hpp
2 // Author: Dirk J. Botha <bothadj@gmail.com>
3 //
4 // This file is part of kisscpp library.
5 //
6 // The kisscpp library is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // The kisscpp library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with the kisscpp library. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef _THREADSAFE_PERSISTED_QUEUE_HPP_
20 #define _THREADSAFE_PERSISTED_QUEUE_HPP_
21 
22 #include <boost/scoped_ptr.hpp>
23 #include <boost/shared_ptr.hpp>
24 #include <boost/thread/mutex.hpp>
25 #include <boost/thread/locks.hpp>
26 #include "persisted_queue.hpp"
27 #include "statable_queue.hpp"
28 
29 //--------------------------------------------------------------------------------
30 namespace kisscpp
31 {
32 
33 template <class _qoT, class _sT>
34 class ThreadsafePersistedQueue : public StatAbleQueue, public boost::noncopyable
35 {
36  public:
37  ThreadsafePersistedQueue(const std::string& queueName,
38  const std::string& queueWorkingDir,
39  const unsigned maxItemsPerPage)
40  {
41  boost::lock_guard<boost::mutex> guard(objectMutex);
42  persistedQ.reset(new PersistedQueue<_qoT, _sT>(queueName, queueWorkingDir, maxItemsPerPage));
43  }
44 
46  {
47  boost::lock_guard<boost::mutex> guard(objectMutex);
48  persistedQ.reset();
49  }
50 
51  void push(boost::shared_ptr<_qoT> p)
52  {
53  boost::lock_guard<boost::mutex> guard(objectMutex);
54  persistedQ->push_back(p);
55  }
56 
57  boost::shared_ptr<_qoT> pop()
58  {
59  boost::lock_guard<boost::mutex> guard(objectMutex);
60  return persistedQ->pop_front();
61  }
62 
63  bool empty()
64  {
65  boost::lock_guard<boost::mutex> guard(objectMutex);
66  return persistedQ->empty();
67  }
68 
69  size_t size()
70  {
71  boost::lock_guard<boost::mutex> guard(objectMutex);
72  return persistedQ->size();
73  }
74 
75  protected:
76  private:
77 
78  boost::scoped_ptr<PersistedQueue<_qoT, _sT> > persistedQ;
79  boost::mutex objectMutex;
80 };
81 
82 }
83 #endif
84 
Definition: persisted_queue.hpp:114
void push(boost::shared_ptr< _qoT > p)
Definition: threadsafe_persisted_queue.hpp:51
~ThreadsafePersistedQueue()
Definition: threadsafe_persisted_queue.hpp:45
boost::shared_ptr< _qoT > pop()
Definition: threadsafe_persisted_queue.hpp:57
size_t size()
Definition: threadsafe_persisted_queue.hpp:69
ThreadsafePersistedQueue(const std::string &queueName, const std::string &queueWorkingDir, const unsigned maxItemsPerPage)
Definition: threadsafe_persisted_queue.hpp:37
bool empty()
Definition: threadsafe_persisted_queue.hpp:63
Definition: statable_queue.hpp:8
Definition: threadsafe_persisted_queue.hpp:34