title: Calling accept() from multiple threads
body: <p>I'm writing a concurrent TCP server that has to handle multiple connections with the <strong>'thread per connection'</strong> approach (using a <strong>thread pool</strong>). My doubt is about which is the most optimal way for every thread to get a different file descriptor.</p>
  
  <p>I found that the next two methods are the most recommended:</p>
  
  <ol>
  <li>A <strong>main thread</strong> that <code>accepts()</code> all the incoming connections and <strong>stores</strong> their descriptors on a data structure (e.g.: a <code>queue</code>). Then every thread is able to get an fd from the queue.</li>
  <li>Accept() is <strong>called directly from every thread</strong>. (Recommended in <a href="https://rads.stackoverflow.com/amzn/click/com/0131411551" rel="noreferrer" rel="nofollow noreferrer">Unix Network Programming V1</a> )</li>
  </ol>
  
  <hr>
  
  <p>Problems I find to each of them:</p>
  
  <ol>
  <li>The static data structure that stores all the fd's must be <strong>locked</strong> (<code>mutex_lock</code>) before a thread can read from it, so in the case that a considerable number of threads wants to read in <strong>exactly the same moment</strong> I don't know how much time would pass until all of them would get their goal.</li>
  <li>I've been reading that the <a href="http://en.wikipedia.org/wiki/Thundering_herd_problem" rel="noreferrer">Thundering Herd</a> problem related to simultaneous <code>accept()</code> calls has not been totally solved on Linux yet, so maybe I would need to create an <strong>artificial solution</strong> to it that would end up making the application at least as slow as with the approach 1. </li>
  </ol>
  
  <hr>
  
  <p><strong>Sources:</strong></p>
  
  <p>(Some links talking about approach 2: <a href="https://stackoverflow.com/questions/2213779/does-the-thundering-herd-problem-exist-on-linux-anymore">does-the-thundering-herd-problem-exist-on-linux-anymore</a> - and one article I found about it (outdated) : <a href="http://www.citi.umich.edu/projects/linux-scalability/reports/accept.html" rel="noreferrer">linux-scalability/reports/accept.html</a> </p>
  
  <p>And an SO answer that recommends approach 1: <a href="https://stackoverflow.com/questions/11488453/can-i-call-accept-for-one-socket-from-several-threads-simultaneously">can-i-call-accept-for-one-socket-from-several-threads-simultaneously</a></p>
  
  <hr>
  
  <p>I'm really interested on the matter, so I will appreciate any opinion about it :)</p>