简体   繁体   English

ZeroMQ-线程数

[英]ZeroMQ - Number of Threads

Q1: How many threads are created when using ZeroMQ? 问题1:使用ZeroMQ时创建了多少个线程?

According to ( output from $ ps -o nlwp <pid> ) the following program creates 3 threads. 根据( $ ps -o nlwp <pid> ),以下程序创建3个线程。

Q2: Why? 问题2:为什么?

Q3: Shouldn't this be just 2 ( one for main, one for I/O-thread, given num_zmq_io_threads == 1 )? Q3:难道这不是2个(给定num_zmq_io_threads == 1 ,一个用于主线程,一个用于I / O线程)?

Q4: Is this documented anywhere? 问题4:此文件记录在任何地方吗?

#include <zmq.hpp>

int main( int argc, char * argv[] )
{
    const int num_zmq_io_threads = 1;        
    zmq::context_t context( num_zmq_io_threads );

    zmq::socket_t socket1( context, ZMQ_PUB );
    socket1.bind( "tcp://127.0.0.1:5555" );

    for(;;)
    {
        // spin
    }

    return 0;
}

ZeroMQ will create a reaper thread to handle socket shutdown, in addition to the number of I/O-threads called for. 除了需要的I / O线程数量外,ZeroMQ将创建一个收割线程来处理套接字关闭。 If you ask for zero I/O-threads, possible when using only intra-processess messages, there will still be a reaper thread. 如果您要求零个I / O线程(仅使用进程内消息时可能),那么仍然会有一个收割线程。 This is why you appear to get one more thread than asked for. 这就是为什么您似乎获得比请求多的线程的原因。

The reason you see no threads when the socket is not created is because ZeroMQ doesn't create the threads on context creation; 未创建套接字时看不到线程的原因是ZeroMQ不在context创建时创建线程。 they are created later. 它们是稍后创建的。 This is more apparent with the ZeroMQ C API, where there are no options when creating the context with zmq_ctx_new() , and then options like number and priority of I/O-threads must be specified after the context is created with zmq_ctx_set() . 这一点在ZeroMQ C API中更加明显,在使用zmq_ctx_new()创建上下文时没有任何选择,然后在使用zmq_ctx_set()创建上下文之后必须指定诸如I / O线程的编号和优先级之类的选项。

Creating the socket triggers ZMQ to create the threads. 创建套接字会触发ZMQ创建线程。 I don't know where it's documented when exactly ZMQ will create threads. 我不知道确切的ZMQ创建线程的时间在哪里记录。 Ie, does it always create all threads when the first socket is created on a context or is it more complex than that? 即,在context创建第一个套接字时,它是否总是创建所有线程? For instance, does it depend on when the socket created has some I/O-operation for a thread to perform? 例如,它是否取决于创建的套接字何时对线程执行一些I / O操作?

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

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