How to allocate local thread storage?

I have a variable in my static function, but I would like it to be static across each thread.

How can I allocate memory for my C ++ class so that each thread has its own copy of the class instance?

AnotherClass::threadSpecificAction() { // How to allocate this with thread local storage? static MyClass *instance = new MyClass(); instance->doSomething(); } 

This is on Linux. I do not use C ++ 0x and this is gcc v3.4.6.

+48
c ++ multithreading new-operator linux thread-local-storage
May 16 '11 at 17:50
source share
9 answers
 #include <boost/thread/tss.hpp> static boost::thread_specific_ptr< MyClass> instance; if( ! instance.get() ) { // first time called by this thread // construct test element to be used in all subsequent calls from this thread instance.reset( new MyClass); } instance->doSomething(); 
+64
May 16 '11 at 18:26
source share

It is worth noting that C ++ 11 represents the thread_local keyword.

Here is an example from Storage Duration Qualifiers :

 #include <iostream> #include <string> #include <thread> #include <mutex> thread_local unsigned int rage = 1; std::mutex cout_mutex; void increase_rage(const std::string& thread_name) { ++rage; std::lock_guard<std::mutex> lock(cout_mutex); std::cout << "Rage counter for " << thread_name << ": " << rage << '\n'; } int main() { std::thread a(increase_rage, "a"), b(increase_rage, "b"); increase_rage("main"); a.join(); b.join(); return 0; } 

Possible output:

 Rage counter for a: 2 Rage counter for main: 2 Rage counter for b: 2 
+61
Mar 29 '13 at 6:01
source share

boost::thread_specific_ptr is the best way to port a solution.

On Linux and GCC, you can use the __thread modifier .

So your instance variable will look like this:

 static __thread MyClass *instance = new MyClass(); 
+12
May 18 '11 at 7:55
source share

If you use Pthreads, you can do the following:

 //declare static data members pthread_key_t AnotherClass::key_value; pthread_once_t AnotherClass::key_init_once = PTHREAD_ONCE_INIT; //declare static function void AnotherClass::init_key() { //while you can pass a NULL as the second argument, you //should pass some valid destrutor function that can properly //delete a pointer for your MyClass pthread_key_create(&key_value, NULL); } void AnotherClass::threadSpecificAction() { //Initialize the key value pthread_once(&key_init_once, init_key); //this is where the thread-specific pointer is obtained //if storage has already been allocated, it won't return NULL MyClass *instance = NULL; if ((instance = (MyClass*)pthread_getspecific(key_value)) == NULL) { instance = new MyClass; pthread_setspecific(key_value, (void*)instance); } instance->doSomething(); } 
+11
May 16 '11 at 18:26
source share

C ++ 11 defines the storage type thread_local , just use it.

 AnotherClass::threadSpecificAction() { thread_local MyClass *instance = new MyClass(); instance->doSomething(); } 

One additional optimization is also to allocate local thread storage.

+4
Nov 23 '15 at
source share

If you work with MSVC ++, you can read Thread Local Storage (TLS)

And then you can see this example .

Also note the TLS Rules and Restrictions

+3
May 16 '11 at 17:59
source share

On Windows, you can use TlsAlloc and TlsFree to distribute storage in a local thread store.

To set and get values ​​using TLS, you can use TlsSetValue and TlsGetValue , respectively

Here you can see an example of how it will be used.

+3
May 16 '11 at 18:04
source share

Just a note ... MSVC ++ supports declspec (thread) from VSC ++ 2005

 #if (_MSC_VER >= 1400) #ifndef thread_local #define thread_local __declspec(thread) #endif #endif 

The main problem (which is solved in boost :: thread_specific_ptr) variables marked by it cannot contain ctor or dtor.

+2
Nov 01 '14 at 3:09
source share

Folly (Facebook Open-Source Library) has a portable implementation of local stream storage.

According to the authors:

Improved local thread storage for non-trivial types (similar speed as pthread_getspecific , but consumes only one pthread_key_t and 4 times faster than boost::thread_specific_ptr ).

If you are looking for a portable implementation of Local Storage Thread, this library is a good option.

0
Jun 22 '16 at 15:35
source share



All Articles