[zeromq-dev] Perennial thread safety question
Charles Remes
lists at chuckremes.com
Tue Apr 9 05:00:41 CEST 2013
+1. The FAQ and guide are scary for a reason. :)
cr
On Apr 8, 2013, at 5:10 PM, Pieter Hintjens <ph at imatix.com> wrote:
> The scary text emerged as the best way to keep people from skimming
> the docs, sharing sockets between threads, and getting crashes. Anyone
> who knows what a full memory barrier is can migrate sockets. The rest
> of us don't do that, and stick to simpler, safer patterns (each thread
> creates the sockets it needs).
>
> -Pieter
>
>
> On Tue, Apr 9, 2013 at 12:04 AM, Matthew Woehlke
> <matthew.woehlke at kitware.com> wrote:
>> On 2013-04-08 16:12, Charles Remes wrote:
>>> On Apr 8, 2013, at 12:40 PM, Matthew Woehlke wrote:
>>>> 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?
>>>
>>> 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).
>>
>> Ah... I think I may have seen that at some point. However I was
>> specifically remembering reading this in the guide:
>>
>> "Don't share ØMQ sockets between threads. ØMQ sockets are not
>> threadsafe. Technically it's possible to migrate a socket from one
>> thread to another but it demands skill. The only place where it's
>> remotely sane to share sockets between threads are in language bindings
>> that need to do magic like garbage collection on sockets."
>>
>> ...which seems to contradict the FAQ. Maybe some less scary text should
>> be used? (As I read the FAQ, it's perfectly safe to migrate a socket
>> just as long as the migration is 'clean', e.g. memory gets synchronized
>> between the last access by thread A and before the first access by
>> thread B.)
>>
>> --
>> Matthew
>>
>> _______________________________________________
>> zeromq-dev mailing list
>> zeromq-dev at lists.zeromq.org
>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
More information about the zeromq-dev
mailing list