[zeromq-dev] inter thread shared q messaging

Martin Sustrik sustrik at fastmq.com
Tue Jul 28 17:10:30 CEST 2009


Hi Ferenc,

> I included source which try to demonstrate what I would like to achieve.
> The main idea is to broadcast message from writer thread to all reader 
> thread. Unfortunatelly this is not a case with the attached source, only 
> one reader thread gets the message.
> 
> Is it really required to put an intermediate "broker thread" to the 
> picture to resolve this issue or there is other way?

Sorry for delay. I've been away for couple of weeks.

You should do the thing other way round.

Define process-scope exchange in the sender thread.

Define local-scope queue in each receiver thread and bind it to the 
exchange defined by the sender thread.

HTH
Martin

> 
> #include <zmq.hpp>
> #include <boost/shared_ptr.hpp>
> #include <boost/thread.hpp>
> 
> zmq::dispatcher_t *dispatcher = new zmq::dispatcher_t(3);
> zmq::locator_t *locator = new zmq::locator_t();
> 
> void th_read()
> {
>      zmq::api_thread_t *api = zmq::api_thread_t::create(dispatcher, 
> locator);
>      api->create_queue("PROCESS_Q", zmq::scope_process);
> 
>      zmq::message_t in_msg;
>      api->receive(&in_msg);
>      std::cout << (unsigned char *)in_msg.data() << std::endl;
> }
> 
> void th_write()
> {
>      zmq::api_thread_t *api = zmq::api_thread_t::create(dispatcher, 
> locator);
>      int eid = api->create_exchange("LOCAL_X");
>      api->bind("LOCAL_X", "PROCESS_Q", NULL, NULL);
> 
>      sleep(1);
>      std::string s("hello");
>      zmq::message_t msg(s.size() + 1);
>      memcpy(msg.data(), s.c_str(), s.size() + 1);
>      api->send(eid, msg);
> }
> 
> int
> main()
> {
>      boost::shared_ptr<boost::thread> reader1(new boost::thread(&th_read));
>      boost::shared_ptr<boost::thread> reader2(new boost::thread(&th_read));
>      boost::shared_ptr<boost::thread> writer(new boost::thread(&th_write));
>      reader1->join();
>      reader2->join();
>      writer->join();
> 
>      return 0;
> }
> 
> --
> Regards
> Ferenc
> _______________________________________________
> 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