title: TLS enumerable_thread_specific in TBB
body: <p>I've been told that enumerable_thread_specific will improve thrad performance but I don't understand why. What is the benefit of using <code>enumerable_thread_specific</code> from the Intel Thread Building Block (TBB) library?</p>
  
  <p>The documentation (<a href="http://www.threadingbuildingblocks.org/docs/help/reference/thread_local_storage/enumerable_thread_specific_cls.htm" rel="nofollow">link</a>) is somewhat hazy in the motivation but seems to indicate that its purpose is to lazily create items in a list in circumstances where you do not know the number of threads in advance as in the TBB documentation example in the link: </p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;utility&gt;
  
  #include "tbb/task_scheduler_init.h"
  #include "tbb/enumerable_thread_specific.h"
  #include "tbb/parallel_for.h"
  #include "tbb/blocked_range.h"
  
  using namespace tbb;
  
  typedef enumerable_thread_specific&lt; std::pair&lt;int,int&gt; &gt; CounterType;
  CounterType MyCounters (std::make_pair(0,0));
  
  struct Body {
       void operator()(const tbb::blocked_range&lt;int&gt; &amp;r) const {
            CounterType::reference my_counter = MyCounters.local();
            ++my_counter.first;
            for (int i = r.begin(); i != r.end(); ++i)
                ++my_counter.second;
       }
  };
  
  int main() {
       parallel_for( blocked_range&lt;int&gt;(0, 100000000), Body());
  
       for (CounterType::const_iterator i = MyCounters.begin();
           i != MyCounters.end(); ++i)
       {
              printf("Thread stats:\n");
              printf("     calls to operator(): %d", i-&gt;first);
              printf("     total # of iterations executed: %d\n\n",
                   i-&gt;second);
      }
  }
  </code></pre>
  
  <p>Is this really necessary and are there other benefits not being listed? It was indicated that there may be advantages for memory access across threads but it's not clear to me how that happens?</p>