首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于zeromq v3.2.3的yqueue.hpp中的push()的谜题

关于zeromq v3.2.3的yqueue.hpp中的push()的谜题
EN

Stack Overflow用户
提问于 2013-11-25 15:09:39
回答 1查看 66关注 0票数 1

最近,我正在尝试阅读zeromq 3.2.3版的源代码。现在我对yqueue.hpp中的push()函数有了一个困惑。

源码是:

代码语言:javascript
复制
//  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,则”返回“。我对此感到困惑。有人能给我解释一下吗?谢谢

EN

回答 1

Stack Overflow用户

发布于 2013-11-26 06:43:55

变量N是类模板的一部分,如下所示:

代码语言:javascript
复制
template <typename T, int N> class yqueue_t

如果你看一下类顶部的注释,你就会得到N的确切含义:

代码语言:javascript
复制
//  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).
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20186566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档