468854 (1) [Avatar] Offline
#1
Hi,

the queue implementation on page 158/159 (Listing 6.4) is not working properly. After one push() and one pop() operation tail still points to an already evicted node. Consequent push() operations will first write to already deleted memory and second do not update head.

Reproduce with:

threadsafe_queue<int> queue;
queue.push(1);
queue.try_pop();
queue.push(2);
std::shared_ptr<int> ptr = queue.try_pop();
if (ptr == nullptr) { std::cout << "nullptr" << std::endl; }
Colin (15) [Avatar] Offline
#2
Yeah, I had the same observation. The problem does not exist when there is a dummy node in the queue, otherwise we need to fix it by setting tail to nullptr if head is nullptr before the return statement in try_pop().
anthony.williams (204) [Avatar] Offline
#3
You're right. It is supposed to clear tail if the last node was removed. Thanks for spotting this.