See here:
What do I get from front() of empty std container?
Bad things happen if you call .front()
on an empty container, better check .empty()
first.
Try:
T pop() {
this->mutex_.lock();
T value;
if( !this->queue_.empty() )
{
value = this->queue_.front(); // undefined behavior if queue_ is empty
// may segfault, may throw, etc.
this->queue_.pop();
}
this->mutex_.unlock();
return value;
}
Note: Since atomic operations are important on this kind of queue, I'd recommend API changes:
bool pop(T &t); // returns false if there was nothing to read.
Better yet, if you're actually using this where it matters, you probably want to mark items in use before deleting in case of failure.
bool peekAndMark(T &t); // allows one "marked" item per thread
void deleteMarked(); // if an item is marked correctly, pops it.
void unmark(); // abandons the mark. (rollback)