31 #ifndef HPP_PINOCCHIO_POOL_HH
32 #define HPP_PINOCCHIO_POOL_HH
34 #include <boost/thread/condition_variable.hpp>
35 #include <boost/thread/mutex.hpp>
62 boost::mutex::scoped_lock lock(mutex_);
63 condVariable_.wait(lock, OneIsAvailable(*
this));
65 std::swap(ret, Ts_[lastFree_]);
74 boost::mutex::scoped_lock lock(mutex_);
76 assert(lastFree_ > 0);
77 assert(Ts_[lastFree_ - 1] == NULL);
78 Ts_[lastFree_ - 1] = t;
80 condVariable_.notify_one();
84 bool available()
const {
return OneIsAvailable(*
this)(); }
86 std::size_t
size()
const {
return Ts_.size(); }
90 boost::mutex::scoped_lock lock(mutex_);
92 throw std::logic_error(
"Cannot free pool when some objects are in use.");
93 for (std::size_t i = 0; i < size(); ++i)
delete Ts_[i];
100 boost::mutex::scoped_lock lock(mutex_);
106 template <
class InputIterator>
107 void push_back(InputIterator first, InputIterator last) {
108 boost::mutex::scoped_lock lock(mutex_);
109 Ts_.insert(Ts_.end(), first, last);
120 struct OneIsAvailable {
122 OneIsAvailable(
const Pool& pool) : p(pool) {}
123 bool operator()() {
return p.lastFree_ < p.
size(); }
127 boost::condition_variable condVariable_;
129 std::size_t lastFree_;
134 #endif // HPP_PINOCCHIO_DEVICE_HH