title: 0MQ: How to use ZeroMQ in a threadsafe manner?
body: <p>I read the <a href="http://zguide.zeromq.org/page:all" rel="noreferrer">ZeroMq guide</a> and I stumbled upon the following:</p>
  
  <blockquote>
    <p>You MUST NOT share ØMQ sockets between
    threads. ØMQ sockets are not
    threadsafe. Technically it's possible
    to do this, but it demands semaphores,
    locks, or mutexes. This will make your
    application slow and fragile. 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.</p>
  </blockquote>
  
  <p>and later on: </p>
  
  <blockquote>
    <p>Remember: <em>Do not use or close sockets except in the thread that created them.</em></p>
  </blockquote>
  
  <p>I also understood that the ZeroMQ <code>Context</code> is threadsafe.</p>
  
  <p>If a class registers for an event of a another class, in .Net, this event might be invoked from a different thread than the thread the listener was created on.</p>
  
  <p>I think there are only two options to be able to dispatch something via ZeroMQ-Sockets from within an eventhandler:</p>
  
  <ul>
  <li>Synchronize the eventhandler-invoking-thread to the thread the ZeroMQ-<code>Socket</code> was created in </li>
  <li>Create a new  ZeroMQ-<code>Socket</code> / get the exisiting  ZeroMQ-<code>Socket</code> for the thread within the eventhandler by using the threadsafe ZeroMQ-<code>Context</code>  </li>
  </ul>
  
  <p>It seems that the 0MQ-Guide to discourage the first one and I don't think that creating a new ZeroMq-Socket for each thread is performant / the way to go.</p>
  
  <p><strong>My Question</strong>:<br>
  What is the correct pattern (the way it is meant to be) to publish messages via 0MQ from within an eventhandler?</p>
  
  <p>Also, did the authors of the guide have the ZeroMQ-Binding for .Net in mind when they wrote:</p>
  
  <blockquote>
    <p>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.  ?</p>
  </blockquote>
  
  <p>Here is some samplecode to emphasize my problem/question:</p>
  
  <pre><code>public class ExampleClass
  {
      public event EventHandler&lt;ByteEventArgs&gt; SomethinIsCalledFromAnotherThread;
  }
  
  public class ByteEventArgs : EventArgs
  {
      public byte[] BytesToSend;
  }
  
  
  public class Dispatcher
  {
      ZMQ.Context ctx;
  
      public Dispatcher(ZMQ.Context mqcontext, ExampleClass exampleClassInstance)
      {
          this.ctx = mqcontext;
          exampleClassInstance.SomethinIsCalledFromAnotherThread += new EventHandler&lt;ByteEventArgs&gt;(exampleClass_SomethinIsCalledFromAnotherThread);
      }
  
      void exampleClass_SomethinIsCalledFromAnotherThread(object sender, ByteEventArgs e)
      {
          // this method might be called by a different thread. So I have to get a new socket etc?
          using (var socket = ctx.Socket(ZMQ.SocketType.PUSH))
          {
              // init socket etc..... and finally: 
              socket.Send(e.BytesToSend);
          }
          // isn't that too much overhead?
      }
  }
  </code></pre>