1.

What Is C++11 Thread Local Storage (thread_local)?

Answer»

A thread_local object comes into existence when a thread starts and is destroyed when the thread ends. Each thread has its own instance of a thread-Local object.

To fully understand the IMPLICATIONS, let's look at an example- here we'll declare a global variable "globalvar" as thread_local. This'll GIVE each thread it's own copy of globalVar and any modifications made to globalVar will only persist inside that particular thread.In the example below, each of the two threads are modifying globalVar – but they are not seeing each other's change, neither is the main thread.

#include "stdafx.h"

#include <string>

#include <thread>

#include <iostream>

#include <functional>

#include <mutex>

using namespace std;

thread_local int globalVar = 0;

mutex mu;

VOID PrettyPrint(int valueToPrint)

{

lock_guard<mutex> lock(mu);

cout << "Value of globalVar in thread " << this_thread::get_id() << " is " << globalVar << endl;

}

void thread_Local_Test_Func(int newVal)

{

globalVar = newVal;

PrettyPrint(globalVar);

}

int main()

{

globalVar = 1;

thread t1(thread_Local_Test_Func, 5);

thread t2(thread_Local_Test_Func, 20);

t1.join();

t2.join();

cout << "Value of globalVar in MAIN thread is " << globalVar << endl;

RETURN 0;

}

Here's the output of the PROGRAM – you can see that the three threads (t1, t2 and MAIN) does not see each other's changes to globalVar.

Value of globalVar in thread 17852 is 5

Value of globalVar in thread 29792 is 20

Value of globalVar in MAIN thread is 1

Can you guess what the output will be if globalVar was not declared thread_local ? Here it is :

Value of globalVar in thread 27200 is 5

Value of globalVar in thread 31312 is 20

Value of globalVar in MAIN thread is 20

If the global value was not thread local, the change made by each thread will be persisted outside the thread – here the MAIN thread is feeling the effect of the change made by t2 and hence printing "20" instead of "1".

A thread_local object comes into existence when a thread starts and is destroyed when the thread ends. Each thread has its own instance of a thread-Local object.

To fully understand the implications, let's look at an example- here we'll declare a global variable "globalvar" as thread_local. This'll give each thread it's own copy of globalVar and any modifications made to globalVar will only persist inside that particular thread.In the example below, each of the two threads are modifying globalVar – but they are not seeing each other's change, neither is the main thread.

#include "stdafx.h"

#include <string>

#include <thread>

#include <iostream>

#include <functional>

#include <mutex>

using namespace std;

thread_local int globalVar = 0;

mutex mu;

void PrettyPrint(int valueToPrint)

{

lock_guard<mutex> lock(mu);

cout << "Value of globalVar in thread " << this_thread::get_id() << " is " << globalVar << endl;

}

void thread_Local_Test_Func(int newVal)

{

globalVar = newVal;

PrettyPrint(globalVar);

}

int main()

{

globalVar = 1;

thread t1(thread_Local_Test_Func, 5);

thread t2(thread_Local_Test_Func, 20);

t1.join();

t2.join();

cout << "Value of globalVar in MAIN thread is " << globalVar << endl;

return 0;

}

Here's the output of the program – you can see that the three threads (t1, t2 and MAIN) does not see each other's changes to globalVar.

Value of globalVar in thread 17852 is 5

Value of globalVar in thread 29792 is 20

Value of globalVar in MAIN thread is 1

Can you guess what the output will be if globalVar was not declared thread_local ? Here it is :

Value of globalVar in thread 27200 is 5

Value of globalVar in thread 31312 is 20

Value of globalVar in MAIN thread is 20

If the global value was not thread local, the change made by each thread will be persisted outside the thread – here the MAIN thread is feeling the effect of the change made by t2 and hence printing "20" instead of "1".



Discussion

No Comment Found