3D Repo Bouncer  1.4
repo_stack.h
1 
22 #pragma once
23 
24 #include <stack>
25 
26 #include <boost/thread.hpp>
27 #include <boost/date_time.hpp>
28 
29 namespace repo{
30  namespace lib{
31  template <class T>
32  class RepoStack
33  {
34  public:
35  RepoStack(){}
36  ~RepoStack(){}
37 
38  void push(const T& item) {
39  boost::mutex::scoped_lock lock(pushMutex);
40  stack.push_back(item);
41  }
42  T pop() {
43  //order potentially matters. Think before shuffling!
44  boost::mutex::scoped_lock popLock(popMutex); //stopping anyone from popping
45  boost::mutex::scoped_lock pushLock(pushMutex); //stopping anyone from pushing
46 
47  while (stack.empty())
48  {
49  //stack is empty. Release the push lock and try again in 50ms
50  pushLock.unlock();
51  boost::this_thread::sleep(boost::posix_time::milliseconds(50));
52  pushLock.lock();
53  }
54 
55  T item = (T)stack.back();
56  stack.pop_back();
57  return item;
58  }
59 
64  std::vector<T> empty()
65  {
66  boost::mutex::scoped_lock popLock(popMutex); //stopping anyone from popping
67  boost::mutex::scoped_lock pushLock(pushMutex); //stopping anyone from pushing
68  std::vector<T> clone = stack;
69  stack.clear();
70  return clone;
71  }
72 
73  private:
74  std::vector<T> stack;
75  mutable boost::mutex pushMutex;
76  mutable boost::mutex popMutex;
77  };
78  }
79 }
Definition: repo_connection_pool_mongo.h:32
std::vector< T > empty()
Definition: repo_stack.h:64
Definition: repo_stack.h:32