C++并发编程实战第6章,代码清单6.4队列实现——单线程版
template<typename T>
class queue
{
private:
struct node
{
T data;
std::unique_ptr<node> next;
node(T data_):
data(std::move(data_))
{}
};
std::unique_ptr<node> head; // 1
node* tail; // 2
public:
queue()
{}
queue(const queue& other)=delete;
queue& operator=(const queue& other)=delete;
std::shared_ptr<T> try_pop()
{
if(!head)
{
return std::shared_ptr<T>();
}
std::shared_ptr<T> const res(
std::make_shared<T>(std::move(head->data)));
std::unique_ptr<node> const old_head=std::move(head);
head=std::move(old_head->next); // 3
return res;
}
void push(T new_value)
{
std::unique_ptr<node> p(new node(std::move(new_value)));
node* const new_tail=p.get();
if(tail)
{
tail->next=std::move(p); // 4
}
else
{
head=std::move(p); // 5
}
tail=new_tail; // 6
}
};
首先,定义数据结构时,head为unique_ptr,tail为原生指针,而不是使用两个unique_ptr,此处意在避免对同一非nullptr地址的两次delete,因为智能指针的默认deleter(std::default_delete)在delete堆后并不会将指针置nullptr,这个没什么问题,大家看的时候可能有疑惑,提出来一下
然后,queue为构造函数函数体为空,配合原生指针类型的tail,显而易见的野指针(当在栈上使用queue时)
最后在try_pop中并没有考虑这样一种情况,当head指向最后一个节点,此时try_pop,tail将指向不合法位置