发布者只在无限循环中发送消息

Publisher only sending messages in infinite loop

我有以下控制流程。在 for 循环中,我制作了一个字符串数组。我将数组的每一行(一个字符串)发送给发布者函数。然后发布者函数应该发送字符串。我有一个问题,如果发布者在无限循环中发布,我的订阅者只接收消息。为什么我不能一直调用publisher函数让它发送数据?为什么它只能在无限循环中工作?

这是我的发布者函数:

    int zedMQserver(std::string output)
     {

     //std ::cout << output.c_str()<< std:: endl;




zmq::context_t context (1);
zmq::socket_t publisher (context, ZMQ_PUB);
publisher.bind("tcp://*:5556");
publisher.bind("ipc://device.ipc");                // Not usable on Windows.


    int i=1;   
    //while(1) {




    //  Send message to all subscribers

  const int SIZE = output.length();

    zmq::message_t message(SIZE+1);
    snprintf ((char *) message.data(), (SIZE+1) ,
        "%s", output.c_str());
    publisher.send(message);
   std :: cout << "sent message" << std:: endl;
//}
return 0;

这是我的订阅者函数:

#include <zmq.hpp>
#include <iostream>
#include <sstream>



int main (int argc, char *argv[])


  {

 zmq::context_t context (1);

 //Socket to talk to server
 std::cout << "Collecting updates from device server...\n" << std::endl;
 zmq::socket_t subscriber (context, ZMQ_SUB);
 subscriber.connect("tcp://localhost:5556");
 const char *filter = (argc > 1)? argv [1]: "";  // no filter currently
 subscriber.setsockopt(ZMQ_SUBSCRIBE, filter, strlen (filter));
 //  Process 100 updates
 zmq::message_t update;

 for (int q =0 ; q<1000 ; q++) {
    subscriber.recv(&update);

    std::string output = std::string(static_cast<char*>(update.data()), update.size());


    std:: cout <<output << std:: endl;       
    }  
 return 0;







  }

为什么当我去掉 while(1) 语句时,我没有收到任何东西?

原来这是经典"slow joiner problem"。我让出版商睡了一会儿,它奏效了。