最近,我正在尝试阅读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,则”返回“。我对此感到困惑。有人能给我解释一下吗?谢谢
发布于 2013-11-26 06:43:55
变量N是类模板的一部分,如下所示:
template <typename T, int N> class yqueue_t如果你看一下类顶部的注释,你就会得到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).https://stackoverflow.com/questions/20186566
复制相似问题