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

Few Questions about TLS

ptomicek
Beginner
289 Views
Hi, I started a small project using a TBB, so far so good, but I ran into few coding and theory problems. If someone coudl find answers to following questions I would be very greatful:
1) Assuming I have a following struct:
[bash]struct myTlsStruct
{
    bool m_someBoolData;
    int m_someData;
};[/bash]
I tried to create enumerable_thread_specific using this struct like this:
[cpp]typedef tbb::enumerable_thread_specific,tbb::ets_key_per_instance> testTLS;[/cpp]
[cpp]
[/cpp]
However I get errors when compiling:
[cpp]enumerable_thread_specific.h(592): error C2064: term does not evaluate to a function taking 0 arguments
1>          c:\users\petr tomicek\documents\visual studio 2010\projects\lua+tbb test\libs\tbb 4.0\include\tbb\enumerable_thread_specific.h(592) : while compiling class template member function 'void tbb::interface6::internal::construct_by_finit::construct(void *)'
1>          with
1>          [
1>              T=myTlsStruct,
1>              Finit=int
1>          ]
1>          c:\users\petr tomicek\documents\visual studio 2010\projects\lua+tbb test\libs\tbb 4.0\include\tbb\enumerable_thread_specific.h(611) : see reference to class template instantiation 'tbb::interface6::internal::construct_by_finit' being compiled
1>          with
1>          [
1>              T=myTlsStruct,
1>              Finit=int
1>          ]
1>          c:\users\petr tomicek\documents\visual studio 2010\projects\lua+tbb test\libs\tbb 4.0\include\tbb\enumerable_thread_specific.h(762) : see reference to class template instantiation 'tbb::interface6::internal::callback_leaf' being compiled
1>          with
1>          [
1>              T=myTlsStruct,
1>              Constructor=tbb::interface6::internal::construct_by_finit
1>          ]
1>          c:\users\petr tomicek\documents\visual studio 2010\projects\lua+tbb test\lua+tbb test\main.cpp(19) : see reference to function template instantiation 'tbb::interface6::enumerable_thread_specific::enumerable_thread_specific(Finit)' being compiled
1>          with
1>          [
1>              T=myTlsStruct,
1>              Allocator=tbb::cache_aligned_allocator,
1>              ETS_key_type=ets_key_per_instance,
1>              Finit=int
1>          ][/cpp]
[cpp]
[/cpp]
What am I doing wrong?
2) If I manually terminate the scheduler and re-initilialize it with lower number of threads then previous inititialization, what happens to TLS? Is the TLS deloeted if their specific thread is removed?
Thank you very much in advance for any help.
0 Kudos
2 Replies
ptomicek
Beginner
289 Views
I have found an answer to my problem, implementing default constructor and overlaoded constuctor solved the problem.
I am still a little bit unclear about TLS elements destruction. If I call clear(), will it call the destructor of the element?
0 Kudos
RafSchietekat
Valued Contributor III
289 Views
1) Part of the code is not there, so we may be missing something essential, and maybe your workaround is not optimal.

2) I don't think that elements are removed when their thread dies. You'll still see them during enumeration. (Hmm, what guarantees that tbb::this_tbb_thread::get_id() does not reuse a value still in use for a thread that does no longer exist?) You could try to do something with task_scheduler_observer to take action when threads enter or exit the TBB world, but this does not cover user threads that access the TLS.

3) You should only call clear() when the TLS is no longer being used concurrently, and it will destroy all elements.

0 Kudos
Reply