[zeromq-dev] ZMQ - Send std::shared_ptr via inproc pub-sub?

Bill Torpey wallstprog at gmail.com
Wed Jun 24 20:20:01 CEST 2020


Serguei:

Your original question implies (but does not state explicitly) that you want to use 0mq to send shared_ptr’s to other threads in the same process.    If that is the case you need to be cognizant of (https://en.cppreference.com/w/cpp/memory/shared_ptr <https://en.cppreference.com/w/cpp/memory/shared_ptr>):

> The ownership of an object can only be shared with another shared_ptr by copy constructing or copy assigning its value to another shared_ptr. Constructing a new shared_ptr using the raw underlying pointer owned by another shared_ptr leads to undefined behavior. 



If that is not the case (i.e., if the destination of the send is not another thread in the same process), you need to actually serialize the data, as suggested by Sergei Nikulov <sergey.nikulov at gmail.com>

Which is it?

B

> On Jun 24, 2020, at 2:06 PM, Rafał Dudycz <rafal.dudycz at gmail.com> wrote:
> 
> Hi,
> 
> Don't you think that sending a pointer to a memory is very error prone? e.g.: object may be destroyed before the receiver gets a serialized copy of shared_ptr as ref_count will not consider the fact that there is an instance of pointer in the ZMQ inproc buffer.
> IMO it would be better to send an object identifier which could be used by a receiver to retrieve data from some data provider.
> 
> --
> Rafal
> 
> 
> śr., 24 cze 2020 o 19:56 Sergei Nikulov <sergey.nikulov at gmail.com <mailto:sergey.nikulov at gmail.com>> napisał(a):
> On Wed, Jun 24, 2020 at 8:11 PM Serguei Khasimkhanov
> <SerKhas at hotmail.com <mailto:SerKhas at hotmail.com>> wrote:
> >
> > Hello all,
> >
> > I have a publisher thread that has a bunch of shared_ptrs to large chunks of data. Occasionally I want to share some of those shared_ptrs with subscribers (for read-only.) The problem is that ZeroMQ only allows sending binary data, not C++ objects. How can I accomplish this?
> 
> You should serialize your object on publisher side -> send -> receive
> -> deserialize on subscriber.
> There is no other way except you doing some interprocess communication.
> Ref. https://www.google.com/search?q=c%2B%2B+serialization <https://www.google.com/search?q=c%2B%2B+serialization>
> 
> >
> > I was thinking I could allocate a copy of the shared_ptr on the heap, then publish a pointer to it, but since I don't know when the subscribers are done using that pointer, I don't know when to deallocate it.
> >
> > - Ser
> > _______________________________________________
> > zeromq-dev mailing list
> > zeromq-dev at lists.zeromq.org <mailto:zeromq-dev at lists.zeromq.org>
> > https://lists.zeromq.org/mailman/listinfo/zeromq-dev <https://lists.zeromq.org/mailman/listinfo/zeromq-dev>
> 
> 
> 
> -- 
> Best Regards,
> Sergei Nikulov
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org <mailto:zeromq-dev at lists.zeromq.org>
> https://lists.zeromq.org/mailman/listinfo/zeromq-dev <https://lists.zeromq.org/mailman/listinfo/zeromq-dev>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> https://lists.zeromq.org/mailman/listinfo/zeromq-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20200624/50d6a3a2/attachment.htm>


More information about the zeromq-dev mailing list