title: Qt how to pass valid socketDescriptor to thread
body: <p>I trying to implement threaded server application which runs 1 processing thread per physical CPU Core/Thread.
  The goal is to spread all incoming connections over threads, threads must close connections if needed.
  My server implementation is based on <strong>QTcpServer</strong> and reimplements <strong>incomingConnection</strong> funtion, which checks current connection counts of all underlayer Threads and pass <strong>qintptr socketDescriptor</strong> to thread-class function, which is only add this <strong>qintptr</strong> to list of "pending sessions".
  In <strong>thread::run</strong> I have infinite loop, this loop is checking list of "pending sessions" and if it is not empty - create new <strong>QTcpSocket</strong> and pass <strong>socketDescriptor</strong> to <strong>setSocketDescriptor</strong> function of newly created Socket object.
  I see in console log what i passing valid <strong>socketDescriptor</strong> (same value as i had in <strong>incomingConnection</strong> function), <em>[wrong information, I just missed "!" in my "if" statement: but setSocketDescriptor failed with error "Unknown Error"]</em> edit: <strong>setSocketDescriptor</strong> function succeed, but Socket dont accept any data and even if connection closed by remote client, <strong>::socketDescriptor()</strong> returns initially passed value (must be -1 after "connection lost" type situation).</p>
  
  <p>Question is: what actually happened with socketDescriptor between calls of incomingConnection and next cycle of thread's loop?
  I found many examples where socketDescriptor successfuly passed to Thread object's constructor function (so socketDescriptor is "used" immediately after incomingConnection), but I need to create Socket objects later in Thread life-cycle.
  I can setup QTcpSocket right in incomingConnection function and it will work. I will try to do so and pass QTcpSocket to desired thread via moveToThread(), but even if it works, I want to know why my current implementation is failed.</p>
  
  <p>code (Main Thread accept connection):</p>
  
  <pre><code>void Server::incomingConnection(qintptr socketDescriptor)
  
  {
      qDebug()&lt;&lt;Q_FUNC_INFO&lt;&lt;"sd:"&lt;&lt;socketDescriptor;
  
      ServerThread* t = threads[0];
  
      // find thread with lowest connection count
      for (unsigned int i = 1; i &lt; CPU_Count; i++)
      {
          if (threads[i]-&gt;getConnectionCount() &lt; t-&gt;getConnectionCount())
              t = threads[i];
      }
  
      t-&gt;newConnection(socketDescriptor);
  }
  </code></pre>
  
  <p>code (pass socketDescriptor to Thread object for further use):</p>
  
  <pre><code>void ServerThread::newConnection(qintptr socketDescriptor)
  {
  // QList&lt;qintptr&gt; sessions_pending;
      sessions_pending.append(socketDescriptor);
  }
  </code></pre>
  
  <p>code (trying to create QTcpSocket in ServerThread::run):</p>
  
  <pre><code>    if (sessions_pending.count())
      {
          foreach (qintptr sd, sessions_pending)
          {
              qDebug()&lt;&lt;"create socket, descriptor : "&lt;&lt;sd; // show same value as incomingConnection
  
              QTcpSocket* newSocket = new QTcpSocket(0);
              if (!newsocket-&gt;setSocketDescriptor(sd))
                  qDebug()&lt;&lt;"Error during socket creation : "&lt;&lt;s-&gt;socket-&gt;errorString()&lt;&lt;s-&gt;socket-&gt;error();
              // output: "Unknown error" QAbstractSocket::UnknownSocketError
  
              // .. here was code which store QTcpSocket and so on ..
          }
          sessions_pending.clear();
      }
  </code></pre>