Last Updated : 15 May, 2023
Thread local storage (TLS) is a feature introduced in C++ 11 that allows each thread in a multi-threaded program to have its own separate instance of a variable. In simple words, we can say that each thread can have its own independent instance of a variable. Each thread can access and modify its own copy of the variable without interfering with other threads.
Syntaxthread_local int my_variable;Properties of Thread Local Storage (TLS)
Example 1:
C++
// C++ Program to implement
// thread_local Storage
#include <iostream>
#include <thread>
using namespace std;
thread_local int counter = 0;
void increment_counter()
{
counter++;
cout << "Thread " << this_thread::get_id()
<< " counter = " << counter << endl;
}
int main()
{
// Create first thread
thread t1(increment_counter);
// Create second thread
thread t2(increment_counter);
// Wait for the first thread to finish
t1.join();
// Wait for the second thread to finish
t2.join();
return 0;
}
Output
Thread 140093779908160 counter = 1 Thread 140093788300864 counter = 1
Example 2:
C++
// C++ Program to demonstrate the use of thread-local
// storage.
#include <iostream>
#include <thread>
using namespace std;
class Singleton {
public:
static Singleton& getInstance()
{
// Each thread will have its own instance of
// Singleton
thread_local Singleton instance;
return instance;
}
void printMessage()
{
cout << "Hello from thread "
<< this_thread::get_id() << endl;
}
private:
Singleton() = default;
};
void workerThread()
{
Singleton::getInstance().printMessage();
}
int main()
{
// Create first thread
thread t1(workerThread);
// Create second thread
thread t2(workerThread);
// Wait for the first thread to finish
t1.join();
// Wait for the second thread to finish
t2.join();
return 0;
}
Output
Hello from thread 139683844367936 Hello from thread 139683852760640Static Thread Local Storage
It allows each thread to have its own separate instance of a static variable. Similar to TLS, each thread can access and modify its own copy of the static variable without affecting other threads but with static storage duration, which means the variable persists across multiple invocations of the function within the same thread.
Example:
C++14
// C++ Program to implement
// Static Thread Local Storage
#include <iostream>
#include <thread>
using namespace std;
void thread_func()
{
// Static thread-local variable
static thread_local int stls_variable = 0;
// Increment the variable
stls_variable += 1;
cout << "Thread ID: " << this_thread::get_id()
<< ", Variable: " << stls_variable
<< endl;
}
int main()
{
thread t1(thread_func);
thread t2(thread_func);
t1.join();
t2.join();
return 0;
}
Output
Thread ID: 140702226085440, Variable: 1 Thread ID: 140702234478144, Variable: 1Rules and limitations
Thread local storage (TLS) in C++ allows each thread to have its own separate instance of a variable. TLS variables have their own lifetime, visibility, and scope within each thread. TLS can be used to maintain thread-specific data without interfering with other threads.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4