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

标签 c++ zeromq

最近,我正在尝试阅读 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/

相关文章:

c++ - AVX 内在澄清,4x4 矩阵乘法奇数

c++ - 为什么 BOOST 创建的这个 JSON 字符串与我的服务器所需的不同?

zeromq - 是否可以从 ZeroMQ PUB 套接字获取过滤器列表?

c# - ZeroMQ (NetMQ) TCP 传输可以在同一进程中的发布者和订阅者之间使用吗?

zeromq - 使用 zmqpp 进行零复制消息传递

c++ - 使用 QStandardItem 传输自定义数据 (Type*) 时内存泄漏?

c++ - 如何正确地将所有权从原始指针移动到 std::unique_ptr?

c++ - 如何定义和测试字符串值的宏?

python - 如何在函数内部(以非阻塞方式)使用 zmq 在客户端请求时获取函数的状态?

c++ - g++ 对 ZMQ 的 undefined reference