🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: de.comp.lang.c
14 messages
14 total messages Started by Bonita Montero Sat, 14 Nov 2026 18:35
Where does my thread-local data live ?
#2
Author: Bonita Montero
Date: Sat, 14 Nov 2026 18:35
17 lines
1754 bytes
#include <Windows.h>
#include <iostream>

using namespace std;

int main()
{
	thread_local int i;
	MEMORY_BASIC_INFORMATION mbi;
	if( !VirtualQueryEx( GetCurrentProcess(), &i, &mbi, sizeof mbi ) )
		return EXIT_FAILURE;
	cout << "allocation base: " << (void *)mbi.AllocationBase << endl;
	cout << "region base:     " << (void *)mbi.BaseAddress << endl;
	cout << "&i:              " << (void *)&i << endl;
	cout << "region size:     " << (double)mbi.RegionSize / 0x1.0p12;
	cout << " pages" << endl;
}
Re: Where does my thread-local data live ?
#3
Author: Bonita Montero
Date: Sat, 14 Nov 2026 19:58
18 lines
2019 bytes
#include <Windows.h
#include <iostream

using namespace std;

int main()
{
      thread_local int i;
      MEMORY_BASIC_INFORMATION mbi;
      if( !VirtualQueryEx( GetCurrentProcess(), &i, &mbi, sizeof mbi ) )
          return EXIT_FAILURE;
      cout << "allocation base: " << (void *)mbi.AllocationBase << endl;
      cout << "region base:     " << (void *)mbi.BaseAddress << endl;
      cout << "&i:              " << (void *)&i << endl;
      cout << "region size:     " << (double)mbi.RegionSize / 0x1.0p12;
      cout << " pages" << endl;
  }
Re: Where does my thread-local data live ?
#4
Author: Bonita Montero
Date: Sun, 15 Nov 2026 08:48
16 lines
2053 bytes
Am 14.11.2026 um 19:35 schrieb Bonita Montero:

> int main()
> {
>      thread_local int i;
>      MEMORY_BASIC_INFORMATION mbi;
>      if( !VirtualQueryEx( GetCurrentProcess(), &i, &mbi, sizeof mbi ) )
>          return EXIT_FAILURE;
>      cout << "allocation base: " << (void *)mbi.AllocationBase << endl;
>      cout << "region base:     " << (void *)mbi.BaseAddress << endl;
>      cout << "&i:              " << (void *)&i << endl;
>      cout << "region size:     " << (double)mbi.RegionSize / 0x1.0p12;
>      cout << " pages" << endl;
> }

Does anyone have a guess what this is good for ? ;-)
Re: Where does my thread-local data live ?
#5
Author: Chris M. Thomasson
Date: Sun, 15 Nov 2026 20:40
23 lines
2207 bytes
On 11/14/2026 10:35 AM, Bonita Montero wrote:
> #include <Windows.h>
> #include <iostream>
> 
> using namespace std;
> 
> int main()
> {
>      thread_local int i;
>      MEMORY_BASIC_INFORMATION mbi;
>      if( !VirtualQueryEx( GetCurrentProcess(), &i, &mbi, sizeof mbi ) )
>          return EXIT_FAILURE;
>      cout << "allocation base: " << (void *)mbi.AllocationBase << endl;
>      cout << "region base:     " << (void *)mbi.BaseAddress << endl;
>      cout << "&i:              " << (void *)&i << endl;
>      cout << "region size:     " << (double)mbi.RegionSize / 0x1.0p12;
>      cout << " pages" << endl;
> }

iirc, in the .tdata or .tss. But that might be for ELF. For Windows, 
damn... iirc, in the TIB?

https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsalloc
Re: Where does my thread-local data live ?
#6
Author: Bonita Montero
Date: Tue, 17 Nov 2026 11:04
13 lines
1842 bytes
Am 15.11.2026 um 21:40 schrieb Chris M. Thomasson:

> iirc, in the .tdata or .tss. But that might be for ELF. For Windows, 
> damn... iirc, in the TIB?

As thread-local data is per thread it isn't in the executable image.
At least for Windows.

> https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-tlsalloc

This is to allocate a TLS-slot for a new image.
You almost never need this.
Re: Where does my thread-local data live ?
#7
Author: Chris M. Thomasson
Date: Wed, 18 Nov 2026 00:09
21 lines
1954 bytes
On 11/17/2026 3:04 AM, Bonita Montero wrote:
> Am 15.11.2026 um 21:40 schrieb Chris M. Thomasson:
> 
>> iirc, in the .tdata or .tss. But that might be for ELF. For Windows, 
>> damn... iirc, in the TIB?
> 
> As thread-local data is per thread it isn't in the executable image.
> At least for Windows.

What about:

__declspec(thread) int x;

> 
>> https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ 
>> nf-processthreadsapi-tlsalloc
> 
> This is to allocate a TLS-slot for a new image.
> You almost never need this.
> 
Re: Where does my thread-local data live ?
#8
Author: Stefan Kanthak
Date: Wed, 18 Nov 2026 06:34
12 lines
1899 bytes
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> wrote:

> What about:
> 
> __declspec(thread) int x;

Take a look at <https://skanthak.hier-im-netz.de/blunder.html#blunder114>
as well as <https://skanthak.hier-im-netz.de/msvcrt.html#tls>

Stefan
-- 
<https://www.duden.de/rechtschreibung/Kanthaken>
Re: Where does my thread-local data live ?
#9
Author: Chris M. Thomasson
Date: Wed, 18 Nov 2026 07:04
12 lines
1839 bytes
On 11/17/2026 10:34 PM, Stefan Kanthak wrote:
> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> wrote:
> 
>> What about:
>>
>> __declspec(thread) int x;
> 
> Take a look at <https://skanthak.hier-im-netz.de/blunder.html#blunder114>
> as well as <https://skanthak.hier-im-netz.de/msvcrt.html#tls>
Oh yeah. For DLL's, TlsAlloc and friends are the way to go. Iirc, in 
DLLMain, set everything up.
Re: Where does my thread-local data live ?
#10
Author: Stefan Kanthak
Date: Wed, 18 Nov 2026 07:30
21 lines
2382 bytes
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> wrote:
> On 11/17/2026 10:34 PM, Stefan Kanthak wrote:
>> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> wrote:
>> 
>>> What about:
>>>
>>> __declspec(thread) int x;
>> 
>> Take a look at <https://skanthak.hier-im-netz.de/blunder.html#blunder114>
>> as well as <https://skanthak.hier-im-netz.de/msvcrt.html#tls>
> Oh yeah. For DLL's, TlsAlloc and friends are the way to go. Iirc, in 
> DLLMain, set everything up.

OUCH: better crawl from under the rock where you stayed 20+ years!
Windows Vista and later versions of NT support __declspec(thread) in
DLLs, there's no need for TlsAlloc() and friends any more in the first
place.

Stefan
-- 
<https://www.duden.de/rechtschreibung/Kanthaken>
Re: Where does my thread-local data live ?
#11
Author: Bonita Montero
Date: Wed, 18 Nov 2026 14:25
12 lines
1809 bytes
Am 18.11.2026 um 01:09 schrieb Chris M. Thomasson:

> On 11/17/2026 3:04 AM, Bonita Montero wrote:

>> As thread-local data is per thread it isn't in the executable image.
>> At least for Windows.

> What about:
> __declspec(thread) int x;

Better use C++11's thread_local since it cares for constructors
and destructors.
Re: Where does my thread-local data live ?
#12
Author: Chris M. Thomasson
Date: Wed, 18 Nov 2026 20:59
16 lines
1988 bytes
On 11/18/2026 6:25 AM, Bonita Montero wrote:
> Am 18.11.2026 um 01:09 schrieb Chris M. Thomasson:
> 
>> On 11/17/2026 3:04 AM, Bonita Montero wrote:
> 
>>> As thread-local data is per thread it isn't in the executable image.
>>> At least for Windows.
> 
>> What about:
>> __declspec(thread) int x;
> 
> Better use C++11's thread_local since it cares for constructors
> and destructors.

Yeah. Also, in a dll: TlsAlloc and friends are the way to go. Iirc, in 
DLLMain, set everything up.
Re: Where does my thread-local data live ?
#13
Author: Bonita Montero
Date: Thu, 19 Nov 2026 15:45
9 lines
1889 bytes
Am 18.11.2026 um 21:59 schrieb Chris M. Thomasson:

> Yeah. Also, in a dll: TlsAlloc and friends are the way to go.
> Iirc, in DLLMain, set everything up.

You can write your own DllMain without honouring TlsAlloc.
The kernel does everything for you and places the right
TLS-index in the thread-information block.
Re: Where does my thread-local data live ?
#14
Author: Chris M. Thomasson
Date: Thu, 19 Nov 2026 20:19
14 lines
2138 bytes
On 11/19/2026 7:45 AM, Bonita Montero wrote:
> Am 18.11.2026 um 21:59 schrieb Chris M. Thomasson:
> 
>> Yeah. Also, in a dll: TlsAlloc and friends are the way to go.
>> Iirc, in DLLMain, set everything up.
> 
> You can write your own DllMain without honouring TlsAlloc.
> The kernel does everything for you and places the right
> TLS-index in the thread-information block.
> 

Its been a while since I created a lot of DLL's in Windows, but:

https://learn.microsoft.com/en-us/windows/win32/dlls/using-thread-local-storage-in-a-dynamic-link-library
Re: Where does my thread-local data live ?
#15
Author: Bonita Montero
Date: Fri, 20 Nov 2026 06:13
9 lines
2044 bytes
Am 19.11.2026 um 21:19 schrieb Chris M. Thomasson:
> Its been a while since I created a lot of DLL's in Windows, but:
> https://learn.microsoft.com/en-us/windows/win32/dlls/using-thread-local- 
> storage-in-a-dynamic-link-library

If you use static TLS, i.e. using __declspec(thread) or thread_local,
you don't need to allocate your own TLS slot. The compiler does that
for you.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads