1

where is the storage of thread_local variables in memory ? Is it heap ?

I have implemented a per-thread basis bootstrap function for our application, something like:

thread_local bool bootstrapped_ = false;
void Bootstrap()
{
    if (bootstrapped_)
        return;

    thread_local ContextTable contexts;
    contexts_ = &contexts;

    thread_local IndexMapper indexer;
    indexer_ = &indexer;
}

ContextTable* Contexts()
{
    if (!bootstrapped_)
        Bootstrap();
    return contexts_;
}

extern IndexMapper* Indexer()
{
    if (!bootstrapped_)
        Bootstrap();
    return indexer_;
}

In the source code above, where is the memory for variables bootstrapped_ , contexts and indexer allocated ?

Monku
  • 2,440
  • 4
  • 33
  • 57
  • 3
    Why the obsession with "physical" location of variables in memory? – Lightness Races in Orbit May 12 '17 at 17:37
  • 1
    Heap and stack are implementation details. – Weak to Enuma Elish May 12 '17 at 17:37
  • @BoundaryImposition If these objects are allocated on StackFrame, then could there be possibility of StackFramSize not enough to hold the objects ? – Monku May 12 '17 at 17:38
  • @Monku: There's an easy way to find out, for your particular system and compiler. – Lightness Races in Orbit May 12 '17 at 17:43
  • 1
    The question asks where the thread local variables are stored. In the case of Windows, they are created outside of the normal heap and flat address space (for 64 bit mode accessed via segment register FS or GS (I don't remember which)). In some (or most?) cases, although the thread variables are effectively in a separate heap, they are mapped into each thread's address space. – rcgldr May 12 '17 at 18:20
  • @BoundaryImposition how is this question a duplicate ? The one you tagged is asking for the meaning. I am not asking that. I know what this storage specifier does. I am asking the details of its underlying implementation. – Monku May 12 '17 at 19:25
  • 1
    @Monku: And, if you read the answers there, you'll find out. – Lightness Races in Orbit May 12 '17 at 19:26
  • @rcgldr: Sometimes, maybe. But even if that were true for a particular implementation, and you could list all the occasions that it's true, it wouldn't be a C++ answer. – Lightness Races in Orbit May 12 '17 at 19:26
  • @BoundaryImposition my bad. got it :) thanks. – Monku May 12 '17 at 19:31

0 Answers0