enumerable_thread_specific object creation C++11 improvement

enumerable_thread_specific object creation C++11 improvement

Hello everyone,

When using enumerable_thread_specific, it can be useful to specify the parameters that will be used when constructing a new object when a thread requests for one.

Two similar possibilities exist in the current implementation :

  • specifying in the constructor of enumerable_thread_specific a function that will return an object that will be copied inside the TLS's storage when a new object is needed
  • specifying in the constructor of enumerable_thread_specific a const-reference to an object that will be copied when creating a new object (by using the copy-ctor of the class T)

The attached patch proposes another approach which stores inside an std::tuple arguments that will be forwarded to the constructor of a new object when one is needed. These arguments are specified in the constructor of the enumerable_thread_specific object. In order not to confuse between this interface and the second one described above, a tag is used as the first parameter of the constructor of enumerable_thread_specific.

Here is a usage example :

struct A
  A(int i, int j): _i(i), _j(j) { }
  int _i;
  int _j;
tbb::enumerable_thread_specific<A> a_tls(tbb::tag_tls_construct_args, 4, 5);

One of the benefits of this approach is that it potentially saves one object copy (which in some cases is important). One drawback is that the argument of the constructor needs to be copyable, and you need to pay attention to the validity of any pointers that could be used.

The issue with this implementation is that it uses C++11 (variadic template arguments and std::tuple) and that it has only be tested with gcc 4.7.2 under Linux (and thus might not be portable).

This post is a kind of feature request with a beggining of implementation ;)

7 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hello Adrien,

Could you pls submit the patch via contribution for on open source site http://threadingbuildingblocks.org/submit-contribution?


Hello Vladimir,

It has been posted !

This would a very welcome extension of the existing implementation, which I too find rather inconvenient.


this contribution was posted over 2 years ago, yet it has not found its way into tbb (at least I cannot find it in the current documentation). I find this very frustrating. It strongly suggests that INTEL is not really interested in contributions from the public.

Are there any plans to still add this valuable contribution?

If not, why not?

I find the current constructors of tbb::enumerable_thread_specific limited as (except for objects with default constructor) it always involves an unnecessary copy (which can be expensive) per thread-specific object.

Do you have evidence that an extra copy is made? I would suppose that a C++11 compiler is capable of copy elision, so you should only have 1+N copies with the prototype-based version, and 0 copies with the function-based version (which could be based on a lambda)? I'm eager to be corrected if I've missed something, though.

(2015-03-24 Added) No answer yet... So what is the perceived problem: the prototype (use the function-based version as an alternative), incomplete copy elision (which environment?), something else?


See CHANGES in 4.3 update 4:

Changes (w.r.t. Intel TBB 4.3 Update 3):
- Added a C++11 variadic constructor for enumerable_thread_specific.
    The arguments from this constructor are used to construct
    thread-local values.
- Improved exception safety for enumerable_thread_specific.
Open-source contributions integrated:
- Draft code for enumerable_thread_specific constructor with multiple
    arguments (see above) by Adrien Guinet.

I'm still curious how this compares to using a lambda function?

Leave a Comment

Please sign in to add a comment. Not a member? Join today