繁体   English   中英

关于zeromq v 3.2.3的yqueue.hpp中的push()的困惑

[英]A puzzle about the push() in yqueue.hpp of zeromq v 3.2.3

最近,我正在尝试阅读3.2.3版的zeromq的源代码。 现在我对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

如果您查看课程顶部的注释,您将确切地了解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).

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM