title: ZMQ Multithread C client
body: <p>I want C example for Multithreaded <code>zmq</code> client, I am already using Multithreaded server, but I have requirements to make each client send requests from multithreads not from a single thread.</p>
  
  <p>I had a look at:</p>
  
  <p><a href="http://zguide.zeromq.org/c:asyncsrv" rel="nofollow noreferrer">http://zguide.zeromq.org/c:asyncsrv</a><br>
  <a href="https://github.com/booksbyus/zguide/blob/master/examples/C/" rel="nofollow noreferrer">https://github.com/booksbyus/zguide/blob/master/examples/C/</a></p>
  
  <p>but I didn't see client example that uses multithreads of <code>ZMQ_DEALER</code> socket talking to Multithreaded server ( a <code>ZMQ_ROUTER</code> socket )</p>
  
  <p>So I am looking for <code>DEALER</code> and <code>ROUTER</code> pattern. I want the client ( the <code>DEALER</code> ) to be multithreaded:</p>
  
  <ul>
  <li>if I follow same analogy of multi-threaded server example, I need a proxy that binds multiple dealers threads, correct?</li>
  </ul>
  
  <p>or</p>
  
  <ul>
  <li>is using a <strong><code>pthread_create</code></strong> enough?</li>
  </ul>
  
  <p>Currently my client is similar to hello world C:</p>
  
  <pre><code>zctx_t ctx = zctx_new ();
  void *client = zsocket_new (ctx, ZMQ_REQ);
  assert (client);
  zsocket_connect (client, config.SERVER_ENDPOINT);
  
  // SKIPPED: I get the data (hm-&gt;body.p) to be send through zmq...
  
  // We send a request, then we work to get a reply
  
  zstr_sendf(client, "%.*s", (int) hm-&gt;body.len,hm-&gt;body.p);
  
  char *reply = zstr_recv (client);
  if (  reply ) {
        zsys_info ("server replied (%s)", reply);
        free (reply);
  } 
  </code></pre>
  
  <p>Please help me make my C client to be a Multithreaded <code>zmq</code> client.</p>
  
  <p><strong>Update 1 (More details):</strong></p>
  
  <ol>
  <li>I have a 3rd party application (let's call it <strong><code>3pa</code></strong> ) that I need to integrate with. The <code>3pa</code> sends on average ~ 4 <strong><code>HTTP POST</code></strong> requests per second ( each request's size ~ 60 KB ) to an HTTP Listener ( I am using mongoose ).</li>
  <li><code>3pa</code> ONLY sends a next <code>HTTP POST</code> after it receives a <code>"HTTP/1.1 200 OK\r\n"</code> "reply" <strong>OR</strong> after a timeout for the sent request. So <code>3pa</code> seems to be single threaded.</li>
  <li>In case I am not able to respond with <strong><code>200 OK</code></strong> quickly, the <code>3pa</code> will keep queuing in Memory, until it crashes.</li>
  <li>When I receive a request from <code>3pa</code>, I need to send it over a 3G-mobile-packet network, to the backend server., using ZMQ. Due to the 3G-mobile-packet network latency and bandwidth, each request requires about 1-3 seconds to be sent, and about 1-3 seconds for a ZMQ replay <code>new ZFrame("OK")</code> to be initiated, transported and delivered (received on the <code>3pa</code> side).</li>
  </ol>
  
  <p>So, if we make the elementary calculus, <code>3pa</code> queue capacity will be filled up so fast due to the 3G-mobile-packet network performance.</p>