[zeromq-dev] Inter thread sharing queue message
Pieter Hintjens
ph at imatix.com
Fri Oct 15 20:56:54 CEST 2010
Hi Kwong,
Have you read the Guide at http://zguide.zeromq.org? This will help
you learn how to do interthread communication using 0MQ.
-Pieter
On Fri, Oct 15, 2010 at 12:50 PM, Kwong Aik Goh <kagohaik85 at hotmail.com> wrote:
> 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;
>> }
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
>
--
-
Pieter Hintjens
iMatix - www.imatix.com
More information about the zeromq-dev
mailing list