I'm implementing my own queue which blocks on .pop(). This function also accepts additional argument which is a timeout. So at the moment I have such code:
template <class T>
class BlockingQueue {
private:
std::queue<T> m_queue;
std::mutex m_mutex;
std::condition_variable m_condition;
public:
T pop(uint64_t t_millis) {
std::unique_lock<std::mutex> lock(m_mutex);
auto status = m_condition.wait_for(
lock,
std::chrono::milliseconds(t_millis),
[=] {
return !m_queue.empty();
}
);
if (!status) {
throw exceptions::Timeout();
}
T next(std::move(m_queue.front()));
m_queue.pop();
return next;
};
}
where exceptions::Timeout is my custom exception. Now I've been thinking about this exception throwing from the performance point of view. Would it be better to return some kind of return code from that function? How does that affect performance?
Also since .pop already returns something how would you implement additional return code? I suppose some new structure that holds both T and a return code would be needed. Is that increase in complexity really worth it?
pop: stackoverflow.com/questions/25035691/…pop()that returns a value and throws an exception? OK, then I guess.boolcalledtry_whatever(non-blocking), then your blockingpop()here. I don't think an exception is necessary here.expected<T>template class, which represents "either, successful construction of a value, or the error that prevented its creation" using e.g.boost::variant<T, error_code>if you want. It's just another style of error handling, it's efficient and doesn't add much complexity, although it might not mesh well with your existing code