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

Can C++11 thread_local be used with TBB

Gennadiy_Rishkin
Beginner
792 Views

Can I expect that C++11 thread_local will have the expected effect. I assume that using thread_local will not cause any surprises since TBB is designed to be a library-based solution that uses the platform's threading. So, for example: 

// Forward declaration of class
Class Test;

// Thread local for thread safety
thread_local *p_global;

...
...
...

int main()
{
    // Create several instances of Test
    // Assign pointer to each instance to p_global;
}

Will every thread have its own copy of p_global?

0 Kudos
1 Solution
RafSchietekat
Valued Contributor III
792 Views

You should be able to use OS-specific threads, tbb::thread and std::thread together, because they all use kernel-based threads underneath, only the API is different. They will all be able to process TBB code in their own arenas. You might even mix APIs for an individual thread if you have a valid need, probably for using the OS-specific API to get specialised access to a thread created with a more generic API. If you're committed to a C++11 environment, it's probably best to replace existing uses of tbb::thread (intended as a compatibility back-porting API) with std::thread.

You should also be able to use the different APIs for thread-local storage independently of the API used to create the thread, but generally you should stick with the same TLS API for an individual variable (I'm not aware of exceptions). Note that TBB TLS has specific functionality that you might want to use that's not available in standard C++, unlike with the tbb::thread API.

View solution in original post

0 Kudos
1 Reply
RafSchietekat
Valued Contributor III
793 Views

You should be able to use OS-specific threads, tbb::thread and std::thread together, because they all use kernel-based threads underneath, only the API is different. They will all be able to process TBB code in their own arenas. You might even mix APIs for an individual thread if you have a valid need, probably for using the OS-specific API to get specialised access to a thread created with a more generic API. If you're committed to a C++11 environment, it's probably best to replace existing uses of tbb::thread (intended as a compatibility back-porting API) with std::thread.

You should also be able to use the different APIs for thread-local storage independently of the API used to create the thread, but generally you should stick with the same TLS API for an individual variable (I'm not aware of exceptions). Note that TBB TLS has specific functionality that you might want to use that's not available in standard C++, unlike with the tbb::thread API.

0 Kudos
Reply