[zeromq-dev] zmq poll failed

Martin Sustrik sustrik at 250bpm.com
Fri Jan 22 07:22:44 CET 2010


Guo,

You should initialise the library with ZMQ_POLL flag:

zmq::context_t ctx (1, 1, ZMQ_POLL);

Here's the relevant part from the man pages:

zmq::context_t

This class encapsulates the functions dealing with initialisation and 
termination of 0MQ context. Constructor of the class invokes zmq_init(3) 
while destructor calls zmq_term(3).

void *zmq_init (int app_threads, int io_threads, int flags);
DESCRIPTION

Initialises 0MQ context. app_threads specifies maximal number of 
application threads that can own open sockets at the same time. At least 
one application thread should be defined. io_threads specifies the size 
of thread pool to handle I/O operations. The value shouldn’t be 
negative. Zero can be used in case only in-process messaging is going to 
be used, i.e. there will be no I/O traffic.

flags argument is a combination of the flags defined below:
ZMQ_POLL

flag specifying that the sockets within this context should be pollable 
(see zmq_poll ). Pollable sockets may add a little latency to the 
message transfer when compared to non-pollable sockets.

Martin

Guo, Yanchao wrote:
> Hi Martin;
> 
> zmq::poll is not working with beta2.0. Below is my testing program and 
> the zmq::poll is always returning -1. I did a bit debugging into the 
> zeromq library, and found there is a check in zmq.cpp, around line 321:
> 
>  pollfds [npollfds].fd = app_thread->get_signaler ()->get_fd ();
>         if (pollfds [npollfds].fd == zmq::retired_fd) {
>             free (pollfds);
>             errno = ENOTSUP;
>             return -1;
>         }
> 
> I believe even for working zeromq socket, the fd is -1 (I printed the 
> value from my test program). Any suggestion on how to fix this?
> 
> #include <zmq.hpp>
> #include "socket_base.hpp"
> #include "app_thread.hpp"
> #include "dispatcher.hpp"
> #include "msg_content.hpp"
> #include "platform.hpp"
> #include "stdint.hpp"
> #include "err.hpp"
> #include "fd.hpp"
> 
> #include <iostream>
> int main (int argc, char** argv)
> {
>         zmq::context_t ctx (1, 1);
>         zmq::socket_t s0 (ctx, ZMQ_SUB);
>         s0.connect ("tcp://172.16.18.21:5700");
>         s0.setsockopt (ZMQ_SUBSCRIBE, "",0);
> 
>         zmq_pollitem_t p[1];
>         p[0].socket = (void*)(s0);
>         std::cout << "addr: " << &s0 << " cast to: " << p[0].socket << 
> std::endl;
>         zmq::socket_base_t *sb = (zmq::socket_base_t*) p[0].socket;
>         int fd = sb->get_thread()->get_signaler()->get_fd();
>         std::cout << "my fd: " << fd << " retired:" << zmq::retired_fd 
> << std::endl; //both value are -1
>         p[0].events = ZMQ_POLLIN;
>         p[0].revents = 0;
>         while (true){
>                 int rc = zmq::poll(p, 1);
> 
>                 if (rc == -1){
>                         std::cout << "poll error" << std::endl; //poll 
> failed because of the above check
>                         sleep(1);
>                 }
>                 else{
>                         for(int i = 0; i<1; i++){
>                                 for(short j = 0; j<p[i].revents; j++){
>                                         zmq::message_t msg;
>                                         s0.recv (&msg);
>                                         std::string 
> abc((char*)msg.data(), msg.size());
>                                         std::cout << "socket: " << i << 
> " event: " << j << " " << abc << std::endl;
>                                         fflush(stdout);
>                                 }
>                         }
>                 }
>         }
>         return 0;
> }
> 
> DISCLAIMER: This e-mail message and any attachments are intended solely 
> for the use of the individual or entity to which it is addressed and may 
> contain information that is confidential or legally privileged. If you 
> are not the intended recipient, you are hereby notified that any 
> dissemination, distribution, copying or other use of this message or its 
> attachments is strictly prohibited. If you have received this message in 
> error, please notify the sender immediately and permanently delete this 
> message and any attachments.
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev




More information about the zeromq-dev mailing list