Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9249679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:15:31+00:00 2026-06-18T10:15:31+00:00

I found a contradiction in MSDN regarding initial values for thread-local storage. This page

  • 0

I found a contradiction in MSDN regarding initial values for thread-local storage. This page says:

When the threads are created, the system allocates an array of LPVOID values for TLS, which are initialized to NULL.

This leads me to believe that if I call TlsGetValue with a valid index from a thread that has never called TlsSetValue for the same index, then I should get a null pointer.

This page, however, says:

It is up to the programmer to ensure … that the thread calls TlsSetValue before calling TlsGetValue.

This suggests that you cannot rely on the value returned from TlsGetValue unless you’re sure it’s been explicitly initialized with TlsSetValue.

Yet the second page simultaneously reinforces the initialized-to-null behavior by also saying:

The data stored in a TLS slot can have a value of 0 because it still has its initial value or because the thread called the TlsSetValue function with 0.

So I’ve got two statements saying that the data is initialized to null (or 0), and one saying that I must initialize it explicitly before reading the value. Experimentally, the values do seem to be initialized to null pointers automatically, but I have no way of knowing if I’m just getting lucky and whether this will always be the case.

I’m trying to avoid using a DLL just to allocate on DLL_THREAD_ATTACH. I’d like to do lazy allocation along the lines of:

LPVOID pMyData = ::TlsGetValue(g_index);
if (pMyData == nullptr) {
  pMyData = /* some allocation and initialization*/;
  // bail out if allocation or initialization failed
  ::TlsSetValue(g_index, pMyData);
}
DoSomethingWith(pMyData);

Is this a reliable and safe pattern? Or do I have to explicitly initialize the slot in each thread before I ever try to read it?

UPDATE: The documentation also says that TlsAlloc zeros out the slots for the allocated index. So whether or not a slot has previously been used by another part of the program seems irrelevant.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:15:33+00:00Added an answer on June 18, 2026 at 10:15 am

    The documentation is being too helpful when it says that the initial value the system allocates for TLS is zero. The statement is true but not useful.

    The reason is that applications can free TLS slots by calling TlsFree, so when you allocate a slot, there is no guarantee that you are the first person ever to be given that slot. Therefore, you don’t know whether the value is the initial 0 assigned by the system or some other junk value assigned by the previous owner of the slot.

    Consider:

    • Component A calls TlsAlloc and gets assigned slot 1. Slot 1 has never been used, so it contains its initial value of 0.
    • Component A calls TlsSetValue(1, someValue).
    • Component A calls TlsGetValue(1) and it gets someValue back.
    • Component A is finished and calls TlsFree(1).
    • Component B calls TlsAlloc and gets assigned slot 1.
    • Component B calls TlsGetValue(1) and gets someValue back because that is the garbage value left behind by component A.

    Therefore, it is up the programmer to ensure that the thread calls TlsSetValue before calling TlsGetValue. Otherwise, your TlsGetValue will read leftover garbage.

    The misleading documentation is saying “The default value of leftover garbage is zero,” but that’s not helpful because you have no idea what happened to the slot between the time the system initialized it and it eventually got given to you.

    Adrian’s follow-up prompted me to study the situation again, and indeed the kernel zeroes out the slot when Component A calls TlsFree(1), so that when component B calls TlsGetValue(1) it gets zero. This assumes that there is not a bug in Component A where it calls TlsSetValue(1) after TlsFree(1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Found this method in System class. Just curious to know about it- public static
Found the answer here (last post): http://social.msdn.microsoft.com/Forums/eu/wcf/thread/f5c0ea22-1d45-484e-b2c0-e3bc9de20915 I'm having one last issue with the
Found this here http://sla.ckers.org/forum/read.php?2,15812,page=2 If you copy the title of the question and run
Found this while reading the Neo4j manual, specifically here , I found the sentence:
Found This: tyty stack, Social Icons not working with Infinite Scrolling on Wordpress I'm
found this little code snippet that seems to do what i want, but im
Found this Multimap containing pairs? , but it is not much help How would
Found this: Sub SurroundWithAppendTag() DTE.ActiveDocument.Selection.Text = .Append( + DTE.ActiveDocument.Selection.Text + ) End Sub But
found this regex: insert every 10 characters: $text = preg_replace(|(.{10})|u, \${1}. , $text); can
Found this rather strange bug in IE8; element.style.top is limited to 1342177 pixels. Even

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.