[zeromq-dev] Inter thread sharing queue message
Kwong Aik Goh
kagohaik85 at hotmail.com
Fri Oct 15 12:50:00 CEST 2010
Hi,
I have two threads running and I would like to send data with each other. Basically I want to write some test code like http://lists.zeromq.org/pipermail/zeromq-dev/2009-July/001057.html in the mailing list. As I understand that there are quite some changes in the code, I have spent quite some time in browsing through the source code to find APIs similar to the example in the mailing list. However, I still couldn't really find anything useful. Could someone help me with this? I would really appreciate it if someone could provide some code on calling the APIs. I have pasted the code from the mailing list below for convenience.
Thanks.
Regards,
Steven Goh
> #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;
> }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20101015/5dc86a75/attachment.htm>
More information about the zeromq-dev
mailing list