Free Web Hosting | free host | Free Web Space | BlueHost Review
Have some fun with Google StreetView

WST 8 - TLS
Home Up Interests Downloads (New) My Resume Links Feedback

 

Interests
Downloads (New)
My Resume
Links
WST
Feedback

Hi all,

Today let's have a look at a very important topic called as Thread Local Storage (TLS). Thread local storage is an area of memory block in each thread, which is unique to that thread. You all must be knowing that the local variables of a thread are unique to each thread, but usually are not accessible to other threads. Similarly, global and static variables are accessible to all threads, but they are not unique to each running thread trying to access them. TLS provides storage area which is unique to each of the threads. The TLS is accessed using indices. The index needs to be allocated once using a function TlsAlloc(). There is a limitation on the number of indexes available for each thread under various OS's. When any thread is started, the OS allocates an array of LPVOID values for TLS these are called TLS slots. Each TLS index is actually an index into this TLS slot array. If data is small enough, you can store it directly into the TLS slot (LPVOID value). If the data is large, you need to allocate buffer for it, and the address of that can be stored in the TLS slot.

The value stored at any TLS slot is retrieved using TlsGetValue() function. The value can be set with TlsSetValue().

When all the threads have terminated, the TLS should be freed (allocated with TlsAlloc()), with TlsFree().

TLS is an extremely useful mechanism, and goes a long way in systems programming. There are many problems in our area, which cannot be solved without using TLS. It's very important that you have good understanding and some hands on experience with the TLS.

Excercises:

1. Read about TLS and TLS associated functions in MSDN.

2. Use TLS for data access in a simple multithreaded application.

3. Find out from MSDN or other sources, how TLS can be extremely helpful and efficient in DLL's being used in multithreaded applications.

That's all for this week. We will try to cover some other topic next week.

I would like to know your feedback on these weekly topics. Are you finding them digestable in one week, are they too complex, are you facing any problems etc.

Do let me know.

Thanks,

-Farooque

 

 

HyperCounter
Bpath Counter