www.digitalmars.com         C & C++   DMDScript  

D - Thread Specific Storage

reply Michael Lindner <mikell optonline.net> writes:
I always thought a nice feature missing from C/C++ was a storage class
for thread specific storage. For instance:

	perthread int x;

would declare an int that had a unique instance in every thread created.
Very easy to implement (just reserve space on the thread's stack before
jumping to the thread's function), but nobody's miplemented it - the
only thread-specific storage interface is the ugly POSIX thing with
unique keys and all sorts of lookups.
Aug 16 2001
parent reply "Matthew Versluys" <mdversluys hotmail.com> writes:
Microsoft has an extension which does this in C++
__declspec(thread)

as well as an API for Thread Local Storage.

Cheers,
Matt.

"Michael Lindner" <mikell optonline.net> wrote in message
news:3B7C8487.89994CFC optonline.net...
 I always thought a nice feature missing from C/C++ was a storage class
 for thread specific storage. For instance:

 perthread int x;

 would declare an int that had a unique instance in every thread created.
 Very easy to implement (just reserve space on the thread's stack before
 jumping to the thread's function), but nobody's miplemented it - the
 only thread-specific storage interface is the ugly POSIX thing with
 unique keys and all sorts of lookups.
Aug 17 2001
parent Christophe de Dinechin <descubes earthlink.net> writes:
Look at the LX thread. To me, that's an instance of the general class of
"pragmas". In LX:

    {thread_specific} int X

There are so many such cases that you don't want it to be part of the
language. To wit, LX language-defined pragmas exist for:

    linkage (Asm, C, etc)
    argument passing conventions
    size and bit size (of fields in structs)
    address of object, or offset in struct
    alignment
    width of memory accesses (*see note 1*)
    memory space for accessing an entitty (I/O, data, for a MC68000, 16, 24
or 32-bit space, etc)
    inlining (specified either at declaration site or call site)
    thread-specific storage
    synchronous access in multi-threaded environments
    symbol to be exported or imported from a DLL
    near or far pointers
    structure packing
    optimization levels
    Whether arguments are passed by value or by reference
    Whether a value has to live in a register, and optionally, in which
register
    Linkage sections (.text, .data, .init_text, etc)
    Whether an object is used only at initialization or only at exit time

And I'm probably forgetting a few. The important point is: the general syntax
and framework allows this list to be extended as needs to be.


(*note 1*) As a programmer of real time systems, I once ran accross the
following interesting problem. Assume ptr is a pointer to a volatile 16-bit
int, I was doing:

    *ptr |= 8

The Microsoft compiler decided: well, 8 fits on 8 bits, right, so I can go
ahead and do a 8-bit OR. Too bad, ptr was pointing to some VME-bus bridge
that took 16-bit accesses and byte-swapped them for me (VME being
good-endian, while x86 is bad-endian). The byte-swapping hardware did not
trigger on the 8-bit access, and I ended up flipping the wrong bit. Talk
about a pain to debug ;-)


Christophe

Matthew Versluys wrote:

 Microsoft has an extension which does this in C++
 __declspec(thread)

 as well as an API for Thread Local Storage.

 Cheers,
 Matt.

 "Michael Lindner" <mikell optonline.net> wrote in message
 news:3B7C8487.89994CFC optonline.net...
 I always thought a nice feature missing from C/C++ was a storage class
 for thread specific storage. For instance:

 perthread int x;

 would declare an int that had a unique instance in every thread created.
 Very easy to implement (just reserve space on the thread's stack before
 jumping to the thread's function), but nobody's miplemented it - the
 only thread-specific storage interface is the ugly POSIX thing with
 unique keys and all sorts of lookups.
Aug 17 2001