title: Is using std::async many times for small tasks performance friendly?
body: <p>To give some background information, I am processing a saved file, and after using a regular expression to split the file into it's component objects, I then need to process the object's data based on which type of object it is.</p>
  
  <p>My current thought is to use parallelism to get a little bit of a performance gain as loading each object is independent of each other. So I was going to define a <code>LoadObject</code> function accepting a <code>std::string</code> for each type of object I'm going to be handling and then calling <code>std::async</code> as follows:</p>
  
  <pre><code>void LoadFromFile( const std::string&amp; szFileName )
  {
       static const std::regex regexObject( "=== ([^=]+) ===\\n((?:.|\\n)*)\\n=== END \\1 ===", std::regex_constants::ECMAScript | std::regex_constants::optimize );
  
       std::ifstream inFile( szFileName );
       inFile.exceptions( std::ifstream::failbit | std::ifstream::badbit );
  
       std::string szFileData( (std::istreambuf_iterator&lt;char&gt;(inFile)), (std::istreambuf_iterator&lt;char&gt;()) );
  
       inFile.close();
  
       std::vector&lt;std::future&lt;void&gt;&gt; vecFutures;
  
       for( std::sregex_iterator itObject( szFileData.cbegin(), szFileData.cend(), regexObject ), end; itObject != end; ++itObject )
       {
            // Determine what type of object we're loading:
            if( (*itObject)[1] == "Type1" )
            {
                 vecFutures.emplace_back( std::async( LoadType1, (*itObject)[2].str() ) );
            }
            else if( (*itObject)[1] == "Type2" )
            {
                 vecFutures.emplace_back( std::async( LoadType2, (*itObject)[2].str() ) );
            }
            else
            {
                 throw std::runtime_error( "Unexpected type encountered whilst reading data file." );
            }
       }
  
       // Make sure all our tasks completed:
       for( auto&amp; future : vecFutures )
       {
             future.get();
       }
  }
  </code></pre>
  
  <p>Note that there will be more than 2 types in the application (this was just a short example) and potentially thousands of objects in the file to be read.</p>
  
  <p>I am aware that creating too many threads is often a bad thing for performance when it exceeds the maximum hardware concurrency due to context switches, but if my memory serves me correctly the C++ runtime is supposed to monitor the number of threads created and schedule <code>std::async</code> appropriately (I believe in Microsoft's case their ConcRT library is responsible for this?), so the above code may still result in a performance improvement?</p>
  
  <p>Thanks in advance!</p>