title: C++ Visual Studio 2010 - Using Threads, Deque And Handlers
body: <p>I'm trying to use multiple threads in an application that reads out of a large file.</p>
  
  <p>I want to read from the file in the main thread and put each line into a deque called <code>countedAddresses</code>.
  The I want multiple threads to pick lines out of <code>countedAddresses</code> and pull all of the email addresses out of them and place them in a deque called <code>resolvedAddresses</code>.</p>
  
  <p>At the moment this all works if there is only one <code>resolveAddressThread</code> running, but if I run two I get heap corruption errors.</p>
  
  <p>I launch the threads with:</p>
  
  <pre><code>_beginthread( resolveAddressThread, 0, NULL );
  _beginthread( resolveAddressThread, 0, NULL );
  </code></pre>
  
  <p>Here is the thread code:</p>
  
  <pre><code>unsigned int linesCounted, linesResolved, linesProcessed;
  std::deque&lt;std::string&gt; countedAddresses;std::deque&lt;std::string&gt; resolvedAddresses;
  HANDLE linesCMutex, linesRMutex, linesRCMutex;
  
  void resolveAddressThread(void* pParams)
  { 
  string fileLine= "";
  
  while(1)
  {
      WaitForSingleObject(linesCMutex, INFINITE);
      WaitForSingleObject(linesRCMutex, INFINITE);
      if (linesCounted&gt;linesResolved)
      {
  
          if (countedAddresses.size()!=0)
          {
              fileLine = countedAddresses.front();
              countedAddresses.pop_front();
              ReleaseMutex(linesRCMutex);
              ReleaseMutex(linesCMutex);
          }
          else
          {
              ReleaseMutex(linesRCMutex);
              ReleaseMutex(linesCMutex);
              Sleep(1000);
  
          }
      }
      else
      { cout&lt;&lt;"All lines in the file have been processed."&lt;&lt;endl;
      ReleaseMutex(linesRCMutex);
      ReleaseMutex(linesCMutex);
      break;}
  
  
      if (fileLine.length()&gt;1)
      {
          string::iterator strIter;
          string buffer;
          int tabCount=0;
          bool useFlag =false, keepFlag = false, stopFlag = false;
          buffer.clear();
          for(strIter=fileLine.begin(); strIter!=fileLine.end(); ++strIter)
          {
              if (*strIter == '\t')
              {
                  ++tabCount;
              }
              if (tabCount == 10)
              {
                  if ('\"'== *strIter)
                  {
                      useFlag=!useFlag;
                  }
                  else if (useFlag &amp;&amp; *strIter &gt; 0)
                  {
  
                      if (*strIter == '@')
                      {
                          buffer+=*strIter;
                          keepFlag = true;
                      }
  
                      else if ((*strIter ==' '||*strIter ==';')&amp;&amp; keepFlag)
                      {
                          if (buffer.at(buffer.length()-1)=='.')
                          {buffer = buffer.substr(0, buffer.length() - 1);}
  
                          WaitForSingleObject(linesRMutex, INFINITE);
                          resolvedAddresses.push_back(buffer);
                          ReleaseMutex(linesRMutex);
                          buffer.clear();
                          keepFlag = false;
  
                      }
                      else if (*strIter ==' '||*strIter ==';')
                      {
                          buffer.clear();
                          keepFlag = false;
                      }
                      else
                      {
                          buffer+=*strIter;
                      }
  
                  }
              }
              if (tabCount ==11 &amp;&amp; keepFlag==true)
              {
                  if (buffer.at(buffer.length()-1)=='.')
                  {buffer = buffer.substr(0, buffer.length() - 1);}
  
                  WaitForSingleObject(linesRMutex, INFINITE);
                  resolvedAddresses.push_back(buffer);
                  ReleaseMutex(linesRMutex);
                  buffer.clear();
                  keepFlag = false;
              }
          }
          WaitForSingleObject(linesRCMutex, INFINITE);
          ++linesResolved;
          ReleaseMutex(linesRCMutex);
          fileLine.clear();
      }
  }
  }
  </code></pre>
  
  <p>Please let me know where I am going wrong.</p>
  
  <p>Thanks,
  ANkh</p>