[zeromq-dev] inter thread shared q messaging

Ferenc Szalai szferi at wsbricks.com
Fri Jul 10 22:10:44 CEST 2009


Hi

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?

#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



More information about the zeromq-dev mailing list