title: How to set a timeout in a function that is running in a thread
body: <p>I'm doing a blocking communication with a server using a client. the function is running in a thread. I would like to set a time out functionality. I'm not using boost or something like that. I'm using windows threading library. </p>
  
  <p>Here is the function that I want to set a time out functionality in it.</p>
  
  <pre><code> bool S3W::IWFSData::WaitForCompletion(unsigned int timeout)
  {
  
      if (m_Buffer)
      {
          while (!m_Buffer.IsEmpty())
          {
              unsigned int i = 0;
              char gfname[255]; // must be changed to SBuffer
              char minHeightArr[8], maxHeightArr[8], xArr[8], yArr[8];
  
              m_PingTime += timeout;
  
              if (m_PingTime &gt; PONG_TIMEOUT)
              {
                  m_PingTime = 0;
                  return false;
              }
  
              while (m_Buffer[i] != '\0')
              {
                  gfname[i] = m_Buffer[i];
                  i++;
              }
  
              gfname[i] = '\0';
  
              for (unsigned int j = 0; j &lt; 8; j++)
              {
                  minHeightArr[j] = m_Buffer[i++];
              }
  
              for (unsigned int j = 0; j &lt; 8; j++)
              {
                  maxHeightArr[j] = m_Buffer[i++];
              }
  
              double minH = *(double*)minHeightArr;
              double maxH = *(double*)maxHeightArr;
  
              for (unsigned int j = 0; j &lt; 8; j++)
              {
                  xArr[j] = m_Buffer[i++];
              }
  
              for (unsigned int j = 0; j &lt; 8; j++)
              {
                  yArr[j] = m_Buffer[i++];
              }
  
              double x = *(double*)xArr;
              double y = *(double*)yArr;
  
              OGRFeature *poFeature = OGRFeature::CreateFeature(m_Layer-&gt;GetLayerDefn());
  
              if(poFeature)
              {
                  poFeature-&gt;SetField("gfname", gfname);
                  poFeature-&gt;SetField("minHeight", minH);
                  poFeature-&gt;SetField("maxHeight", maxH);
  
                  OGRPoint point;
                  point.setX(x);
                  point.setY(y);
  
                  poFeature-&gt;SetGeometry(&amp;point);
  
  
                  if (m_Layer-&gt;CreateFeature(poFeature) != OGRERR_NONE)
                  {
                      std::cout &lt;&lt; "error inserting an area" &lt;&lt; std::endl;
                  }
                  else
                  {
                      std::cout &lt;&lt; "Created a feature" &lt;&lt; std::endl;
                  }
              }
  
              OGRFeature::DestroyFeature(poFeature);
  
              m_Buffer.Cut(0, i);
          }
      } 
  
      return true;
  }
  </code></pre>
  
  <p>There is a thread that is setting the data to the buffer</p>
  
  <pre><code>int S3W::ImplConnection::Thread(void * pData)
  {
     SNet::SAutoLock lockReader(m_sLock);
      // RECEIVE DATA
     SNet::SBuffer buffer;
     m_data-&gt;SrvReceive(buffer);
  
  
    // Driver code for inserting data into the buffer in blocking communication 
     SNet::SAutoLock lockWriter(m_sLockWriter);
     m_data-&gt;SetData("ahmed", strlen("ahmed"));
     double minHeight = 10;
     double maxHeight = 11;
     double x = 4;
     double y = 2;
     char minHeightArr[sizeof(minHeight)];
     memcpy(&amp;minHeightArr, &amp;minHeight, sizeof(minHeight));
  
  
     char maxHeightArr[sizeof(maxHeight)];
     memcpy(&amp;maxHeightArr, &amp;maxHeight, sizeof(maxHeight));
  
  
     char xArr[sizeof(x)];
     memcpy(&amp;xArr, &amp;x, sizeof(x));
  
  
     char yArr[sizeof(y)];
     memcpy(&amp;yArr, &amp;y, sizeof(y));
  
     m_data-&gt;SetData(minHeightArr, sizeof(minHeightArr));
     m_data-&gt;SetData(maxHeightArr, sizeof(maxHeightArr));
     m_data-&gt;SetData(xArr, sizeof(xArr));
     m_data-&gt;SetData(yArr, sizeof(yArr));
  
     m_data-&gt;WaitForCompletion(1000);
  
  
      return LOOP_TIME;
  }
  </code></pre>