gpt4 book ai didi

c++ - 关于zeromq v 3.2.3的yqueue.hpp中push()的一个谜题

转载 作者:太空宇宙 更新时间:2023-11-04 13:58:47 27 4
gpt4 key购买 nike

最近,我正在尝试阅读 zeromq 3.2.3 版本的源代码。现在我对 yqueue.hpp 中的 push() 函数感到困惑?

源代码是:

//  Adds an element to the back end of the queue.
inline void push ()
{
back_chunk = end_chunk;
back_pos = end_pos;

if (++end_pos != N)
return;

chunk_t *sc = spare_chunk.xchg (NULL);
if (sc) {
end_chunk->next = sc;
sc->prev = end_chunk;
} else {
end_chunk->next = (chunk_t*) malloc (sizeof (chunk_t));
alloc_assert (end_chunk->next);
end_chunk->next->prev = end_chunk;
}
end_chunk = end_chunk->next;
end_pos = 0;
}

不断移动“end_pos”的位置,如果不等于N,“返回”。我对此感到困惑。有人可以为我解释一下吗?谢谢

最佳答案

变量 N 是此处定义的类模板的一部分:

template <typename T, int N> class yqueue_t

如果您查看类(class)顶部的评论,您就会明白 N 的确切含义:

//  yqueue is an efficient queue implementation. The main goal is
// to minimise number of allocations/deallocations needed. Thus yqueue
// allocates/deallocates elements in batches of N.
//
// yqueue allows one thread to use push/back function and another one
// to use pop/front functions. However, user must ensure that there's no
// pop on the empty queue and that both threads don't access the same
// element in unsynchronised manner.
//
// T is the type of the object in the queue.
// N is granularity of the queue (how many pushes have to be done till
// actual memory allocation is required).

关于c++ - 关于zeromq v 3.2.3的yqueue.hpp中push()的一个谜题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20186566/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com