bool frontBlock(T& t, duration_type timeout) const { boost::mutex::scoped_lock lock(mutex_); bool gotValue = false; if (timeout.count() > 0) { if (deque_.empty()) { available_.wait_for(lock, timeout); } if (!deque_.empty()) { t = deque_.front(); gotValue = true; } } else { while (deque_.empty()) { available_.wait(lock); } t = deque_.front(); gotValue = true; } return gotValue; }
/// Estimate the necessary number of buckets std::size_t bucket_count( duration_type measurement_period, duration_type sampling_period) { if (sampling_period <= duration_type(0)) { std::ostringstream os; os << "jb::event_rate_estimate - sampling period (" << sampling_period.count() << ") must be a positive number"; throw std::invalid_argument(os.str()); } if (sampling_period > measurement_period) { std::ostringstream os; os << "jb::event_rate_estimate - measurement period (" << measurement_period.count() << ") is smaller than sampling period (" << sampling_period.count() << ")"; throw std::invalid_argument(os.str()); } if ((measurement_period % sampling_period).count() != 0) { std::ostringstream os; os << "jb::event_rate_estimate - measurement period (" << measurement_period.count() << ") must be a multiple of the sampling period (" << sampling_period.count() << ")"; throw std::invalid_argument(os.str()); } // ... because measurement_period and sampling_period are positive // numbers N will be a positive number ... typedef typename duration_type::rep rep; typedef typename std::make_unsigned<rep>::type unsigned_rep; unsigned_rep N = measurement_period / sampling_period; // ... beware, the type may be larger than what can be stored in // an std::size_t (weird segmented memory platforms, yuck) ... if (N >= std::numeric_limits<std::size_t>::max()) { std::ostringstream os; os << "jb::event_rate_estimate - measurement period (" << measurement_period.count() << ") is too large for sampling period (" << sampling_period.count() << ")"; throw std::invalid_argument(os.str()); } return static_cast<std::size_t>(N); }