[zeromq-dev] clrzmq - sending kill request to other threads

Johnny Gozde johnny at jgoz.net
Sun Feb 26 07:56:45 CET 2012


Hi Seif,

On Sat, Feb 25, 2012 at 4:23 PM, Seif Attar <iam at seifattar.net> wrote:

> I am trying to write a logger for NHibernate that will pulish log
> messages with 0mq, each logger might be used from a different thread.
>

ZMQ sockets are not thread safe, so you will need to synchronize all access
to the Logger sockets. It looks like you are not doing this right now, so
you should lock the socket when calling 'Send' in your Publish method.


> This works ok, only problem I am having is that I can't dispose of the
> objects properly, I need a way to send a kill signal to the loggers
> before terminating the context. <snip>
>
> Any recommendations on how to go about this? The code is on github:
>

Since all of the sockets you need to coordinate are in a single process,
you may be able to do this without kill signals. You could create a Context
wrapper with a "Socket" method that keeps track of all the sockets it
creates (in a List or something). The wrapper would also expose an
"IsTerminated" property and a "Terminate()" method.

Then, when you call wrapper.Terminate(), it iterates through the list of
sockets, locking on each one and closing/disposing it. After that, it
disposes the actual context object.

Inside the Logger **and inside the lock on the socket**, you would have to
check the context.IsTerminated property before sending. It's important to
put this check inside the critical region because the Logger may be waiting
to send while the socket is being disposed. In theory, this allows you to
remove the try..catch block that checks for ETERM.

Hopefully that makes sense.


> I had tried opening a new Socket in each ZMQLogger.Publish call, but
> that caused an exception (Too many files open), understandble, so I am
> back at  having to send the kill signal.
>

This is a limitation of ZMQ on Windows. Internally, the OS select() method
is used for socket operations, which limits the open socket count to 1024.

Forgot to mention the errors I am getting, I get 2 exceptions at
> different times:


These are almost certainly caused by multiple threads accessing the Logger
sockets, as you describe above.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120225/3e46309d/attachment.htm>


More information about the zeromq-dev mailing list