简体   繁体   中英

How do I use correctly multi threads in C++ and ZMQ

I'm doing some message passing between a Core Module in C++, which has to comunicate with a Python Module and a Graphics interface

I'm doing the messaging using ZMQ in the Following fashion:

int main()
{
    context_t context(1);
    socket_t publisher(context, ZMQ_PUB);
    publisher.bind("tcp://127.0.0.1:50000");
//This sockets takes charge of the python publishing
    socket_t send_py(context, ZMQ_SUB);
    send_py.bind("tcp://127.0.0.1:5557");
    socket_t receive_py(context, ZMQ_SUB);
    receive_py.connect("tcp://127.0.0.1:5558");
    receive_py.setsockopt(ZMQ_SUBSCRIBE, NULL, 0);
    zmq::message_t control_signal(sizeof(float));
    zmq::message_t control_signal_second(sizeof(float));
    cout<<"flag"<<endl;
    publisher.send(control_signal);
    cout<<"flag_1"<<endl;
    send_py.send(control_signal_second);
    cout<<"flag_2"<<endl

}

I already toyed with the addresses and I'm sure the ports open.

The code compiles, but I get the following output:

flag
flag_1
terminate called after throwing an instance of 'zmq::error_t'
  what():  Operation not supported
Aborted (core dumped)

Is publishing correctly one of them, but is not letting the send_pyu.send work.

Thanks a lot

It appears you've defined send_py as a subscriber:

socket_t send_py(context, ZMQ_SUB);
send_py.bind("tcp://127.0.0.1:5557");

..but you're trying to send data on it, which is invalid and not supported; subscribers receive, they don't send data.

send_py.send(control_signal_second);

If you need to send data, use PUB , PUSH , or some other socket type, but SUB won't work with send() ; it will throw Operation Not Supported .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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