[zeromq-dev] Perennial thread safety question
Charles Remes
lists at chuckremes.com
Mon Apr 8 22:12:27 CEST 2013
On Apr 8, 2013, at 12:40 PM, Matthew Woehlke <matthew.woehlke at kitware.com> wrote:
> On 2013-04-08 08:37, Pieter Hintjens wrote:
>> So "sharing sockets between threads" is really a non-question, it's
>> only something people will try when they have not yet understood how
>> to use 0MQ properly. The one exception to this general rule is in
>> esoteric cases where you may create a socket in one thread, for use by
>> an other thread.
>
> I'm actually doing this. Essentially, I have a 'thread class' that has a
> run() method that is a thread entry point, but the ctor/dtor execute in
> the 'parent' thread.
>
> Is it safe to create and set up, and tear down, my 0MQ socket in the
> ctor/dtor (i.e. in thread A, and poll/recv/etc. in thread B)? Is there
> something I should be doing to make it safe?
>
> The class does guarantee that ctor/run()/dtor will never execute in
> parallel.
This is answered in the FAQ (http://www.zeromq.org/area:faq). Short answer is: Yes, go ahead and do this. If it crashes, protect the socket with some kind of memory barrier (e.g. mutex).
cr
More information about the zeromq-dev
mailing list