Link to home
Start Free TrialLog in
Avatar of JRPrakash
JRPrakash

asked on

Why thread specific data got introduced?

1) See, i did not get the clear concept behind of having thread specific data ?
It could be nice if someone can explain me why?.  I see the same can be achieved
by global value from main thread which can be shared between all threads.

2) Do u have any design example where this thread specific data fits into ?
Avatar of Narendra Kumar S S
Narendra Kumar S S
Flag of India image

> I see the same can be achieved by global value from main thread which can be shared between all
> threads
One major disadvantage of the global data is, you will have to always use a synchronization parameter such as mutex, so that only one thread modifies that global variable at a time.
So, you will get to a stage, where you want to keep something private to a thread and this can be achieved by having thread specific data.
Avatar of JRPrakash
JRPrakash

ASKER

"One major disadvantage of the global data is, you will have to always use a synchronization parameter such as mutex, so that only one thread modifies that global variable at a time.
So, you will get to a stage, where you want to keep something private to a thread and this can be achieved by having thread specific data."


Well,  but the same think can be achieved by local variables defining inside the thread correct ?.
ASKER CERTIFIED SOLUTION
Avatar of Narendra Kumar S S
Narendra Kumar S S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> Well,  but the same think can be achieved by local variables defining inside the thread correct ?
But, thread-specific data is also global data for the program.
But, every thread has a private copy.
For example, you need a global variable and that will be accessed by different functions.
If you make all the global variables as local variables inside a function, then communication between different functions become cumbersome.
So, you cannot avoid having global variables and that is needed in programming.
Now, if multiple threads are modifying the same global variable, then that becomes a problem, right?
This can be solved by thread-specific data.
Thread specific data is not global data for the program.  That's the whole point of it, its specific to each thread.  Local inside a function only exist inside the function, they cannot persist like global variables.  However global variables can be accessed by any thread without synchronisation.

Thread specific data gives each thread its own memory space to work with.  It can allocate and free memory without needing to concern about sharing with other threads.

In practice, thread specific data is very useful.  Imagine you are writing a DLL that has functions which reads an image from a file into memory, manipulate the image in a variety of ways, save the image, free the memory buffer.  Suppose a program creates two threads that use the DLL to process two different files at the same time.  Each thread uses the DLL to load the image into a different memory buffer, the DLL needs a pointer to a memory buffer which is different for each thread.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.