Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

enumerable_thread_specific object creation C++11 improvement

Adrien_Guinet
Beginner
527 Views

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 :

[cpp]
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);
[/cpp]

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 ;)

0 Kudos
6 Replies
Vladimir_P_1234567890
527 Views

Hello Adrien,

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

thanks
--Vladimir 

0 Kudos
Adrien_Guinet
Beginner
527 Views

Hello Vladimir,

It has been posted !

0 Kudos
Walter_D_
Beginner
527 Views

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

0 Kudos
Walter_D_
Beginner
527 Views

Hi,

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.

0 Kudos
RafSchietekat
Valued Contributor III
527 Views

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?

 

0 Kudos
RafSchietekat
Valued Contributor III
527 Views

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?

0 Kudos
Reply