title: Control degree of parallelism with std::async
body: <p>Is there a way to explicitly set/limit the degree of parallelism (= the number of separate threads) used by <code>std::async</code> and related classes?</p>
  
  <p><a href="http://en.cppreference.com/w/cpp/thread" rel="noreferrer">Perusing the thread support library</a> hasn’t turned up anything promising.</p>
  
  <p>As close as I could figure out, <code>std::async</code> implementations (usually?) use a thread pool internally. Is there are standardised API to control this?</p>
  
  <p>For background: I’m in a setting (shared cluster) where I have to manually limit the number of cores used. If I fail to do this, the load sharing scheduler throws a fit and I’m penalised. In particular, <code>std::thread::hardware_concurrency()</code> holds no useful information, since the number of physical cores is irrelevant for the constraints I’m under.</p>
  
  <p>Here’s a relevant piece of code (which, in C++17 with parallelism TS, would probably be written using <a href="http://en.cppreference.com/w/cpp/algorithm/transform" rel="noreferrer">parallel <code>std::transform</code></a>):</p>
  
  <pre><code>auto read_data(std::string const&amp;) -&gt; std::string;
  
  auto multi_read_data(std::vector&lt;std::string&gt; const&amp; filenames, int ncores = 2) -&gt; std::vector&lt;std::string&gt; {
      auto futures = std::vector&lt;std::future&lt;std::string&gt;&gt;{};
  
      // Haha, I wish.
      std::thread_pool::set_max_parallelism(ncores);
  
      for (auto const&amp; filename : filenames) {
          futures.push_back(std::async(std::launch::async, read_data, filename));
      }
  
      auto ret = std::vector&lt;std::string&gt;(filenames.size());
      std::transform(futures.begin(), futures.end(), ret.begin(),
              [](std::future&lt;std::string&gt;&amp; f) {return f.get();});
      return ret;
  }
  </code></pre>
  
  <p>From a design point of view I’d have expected the <code>std::execution::parallel_policy</code> class (from parallelism TS) to allow specifying that (in fact, this is how I did it in the framework I designed for my master thesis). But this doesn’t seem to be the case.</p>
  
  <p>Ideally I’d like a solution for C++11 but if there’s one for later versions I would still like to know about it (though I can’t use it).</p>