Re: thread specific information



Rahul wrote:
Hi Everyone,

I was working with threads and wanted to know which segments are
specific to each thread?

like stack, program counter and other registers, heap, data segment
(bss)...

Thanks in advance!!!

Depends a lot on what you mean by "specific to each thread". There's ACTUALLY thread-specific and EFFECTIVELY thread-specific.

The only things ACTUALLY thread-specific are the hardware registers. Even those are kinda loose definitions of "actual", if you take "thread specific" to the level of "there's absolutely no physical means by which another thread can change them", because when a thread's not active those register values need to be saved somewhere in memory.

POSIX requires that all memory is globally addressable, by all threads. Windows threading follows the same model, as do most common proprietary threading models (e.g. the archaic UI threads, Mach cthreads, etc.)

Stacks, and the thread context switch data areas, are EFFECTIVELY thread specific. There's rarely any reason or need for another thread to touch them, but it's not physically impossible. In fact many real world programming techniques require threads to access stack data allocated by another thread. It's just a matter of passing addresses. (Many real world BUGS also require threads to access stack data allocated by another thread -- usually by accident. Some of these bugs are directly related to the aforementioned "programming techniques", so such things should always be viewed with caution. ;-) )

Heap is at best "casually" thread-specific. That is, one thread allocates heap and retains a pointer to it. If it keeps that pointer to itself there's no reason for another thread to access it, except by accident. But if you store the pointer in a globally accessible place, any other thread CAN access it -- and this is critical to thread programming. After all, threads are intended to be COOPERATIVE, and to cooperate they need to communicate. Communication through statically allocated data is rather limited, so sharing heap is important.

All static program data (text, data, bss, etc) are globally accessible by label. You can run the code in any thread, you can access extern/static data in any thread. That of course means that all such data must be protected by some synchronization scheme. (As simple or complex as required by the application.)

POSIX provides the "thread specific data" (TSD) mechanism by which any application can effectively declare a global "key" by which each thread can reference it's own private value -- generally a pointer to some (heap) allocation. But again, the allocated memory itself is globally visible; only the key value is really private, and at that usually only "effectively" private.

Less standard, but essentially universal now, is "thread local storage" (TLS), a compiler/loader/runtime mechanism that allows a special declaration for data with private instantiation for each thread. This may be implemented on top of TSD, or even the other way around, but it can be more convenient. There's just (as yet) no portable syntax for declaring it. Again, this effectively allocates GLOBALLY visible memory to which only one thread is (by default) granted a pointer; but there's nothing to prevent that thread from making a pointer visible to other threads, or to keep other threads from accidentally "scribbling" over the data via a random uninitialized pointer.
.



Relevant Pages

  • Re: [announcement] SYSAPI and SYSSVC for Windows
    ... That's not a bad design to create a pointer to a instance of an object. ... It's a perfectly normal C++ way of programming. ... The compiler would simply take those parameters as a string literal, ... which string literals are not. ...
    (comp.lang.ada)
  • Pointers and Teaching C++ [was Re: A little disappointed]
    ... like Java where *many* facilities such as raw pointer access / manipulation ... are removed in order to create a 'safer' language and stop programmers ... then it is probably wiser to teach programming using ... pointer access / manipulation is not a 'high level language'. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Garbage collectable pinned arrays!
    ... Pinning is an explicit ... I've already given two examples of APIs in widespread use which require a buffer to stay in one position after the initial function call which accepts the pointer. ... That means a one time cost to pin a buffer that lives until the end of the process, if you do this early in the process you won't suffer from fragmentation of the gen0 heap as this object will end on the gen2 heap anyway. ... If this doesn't suits your needs, then you will have to use the Marshal class or GCHandle.Alloc, carefully considering it's costs. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ptrs validity
    ... I have a pointer that points to an unknown heap memory block, ... hardware checked segment for each allocation. ...
    (comp.lang.c)
  • Re: Briatore hits out on cheating by certain teams
    ... char* s = calloc); ... spent five years lecturing on introductory programming in C++ to CS ... the compiler might even optimise away the ... promotable to any other pointer) doesn't require casting...except in C++ ...
    (rec.autos.sport.f1)