Can C++11 thread_local be used with TBB

Can C++11 thread_local be used with TBB

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?

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

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.

Leave a Comment

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