Tags: equivalent, familiar, linux, linuxthank, local, programming, storage, threading, tls, totls, unix, win32

thread local storage (tls)

On Programmer » Unix & Linux

8,409 words with 5 Comments; publish: Tue, 29 Apr 2008 19:42:00 GMT; (20046.88, « »)

for those familiar with win32 threading, is there something equivalent to

TLS in unix/linux?

Thank you for your help

JohnP

All Comments

Leave a comment...

  • 5 Comments
    • JohnP wrote:

      > for those familiar with win32 threading, is there something equivalent to

      > TLS in unix/linux?

      >

      > Thank you for your help

      >

      > JohnP

      >

      lookup stuff about pthread_key_t and pthread_setspecific()

      #2; Tue, 29 Apr 2008 19:44:00 GMT
    • red floyd wrote:

      > JohnP wrote:

      >

      > lookup stuff about pthread_key_t and pthread_setspecific()

      That's usually called "thread-specific data" (TSD), while

      TLS ("thread-local storage") is a bit different. I don't think

      any POSIX standard covers TLS as yet (but I may be out-of-date),

      so there may be significant differences between systems in how

      TLS is used and implemented.

      Eric.Sosman.unix-linux.questionfor.info.sun.com

      #3; Tue, 29 Apr 2008 19:45:00 GMT
    • Eric Sosman wrote:

      > That's usually called "thread-specific data" (TSD), while TLS

      > ("thread-local storage") is a bit different.

      no, that's exactly what JohnP asked for. I'm not sure what do you mean

      with TLS, but the Windows equivalents of pthread_setspecific are called

      Tls<etc>

      JohnP: there's a POSIX threads library for Windows, maybe you can

      extract useful information from its sources. Here is it:

      <http://sources.redhat.com/pthreads-win32/>

      #4; Tue, 29 Apr 2008 19:46:00 GMT
    • Eric Sosman <Eric.Sosman.unix-linux.questionfor.info.sun.com> writes:

      > red floyd wrote:

      > That's usually called "thread-specific data" (TSD), while TLS

      > ("thread-local storage") is a bit different.

      On GNU/Linux systems, TLS is used like this:

      __thread int foobar; /* foobar is thread-local */

      int *fooptr = &foobar; /* pointer resolved at runtime to current

      thread's instance */

      I can't compare it with Windows TLS, since I've never seen it (I've

      not used it on Linux yet, mind).

      > I don't think any POSIX standard covers TLS as yet (but I may be

      > out-of-date), so there may be significant differences between

      > systems in how TLS is used and implemented.

      It's covered by an amendment to C99 and C++98, so it looks like it's

      going to be standard across all compliant implementations. From the

      GCC manual:

      5.48.1 ISO/IEC 9899:1999 Edits for Thread-Local Storage

      ----

      The following are a set of changes to ISO/IEC 9899:1999 (aka C99) that

      document the exact semantics of the language extension.

      * `5.1.2 Execution environments'

      Add new text after paragraph 1

      Within either execution environment, a "thread" is a flow of

      control within a program. It is implementation defined

      whether or not there may be more than one thread associated

      with a program. It is implementation defined how threads

      beyond the first are created, the name and type of the

      function called at thread startup, and how threads may be

      terminated. However, objects with thread storage duration

      shall be initialized before thread startup.

      * `6.2.4 Storage durations of objects'

      Add new text before paragraph 3

      An object whose identifier is declared with the storage-class

      specifier `__thread' has "thread storage duration". Its

      lifetime is the entire execution of the thread, and its

      stored value is initialized only once, prior to thread

      startup.

      * `6.4.1 Keywords'

      Add `__thread'.

      * `6.7.1 Storage-class specifiers'

      Add `__thread' to the list of storage class specifiers in

      paragraph 1.

      Change paragraph 2 to

      With the exception of `__thread', at most one storage-class

      specifier may be given [...]. The `__thread' specifier may

      be used alone, or immediately following `extern' or `static'.

      Add new text after paragraph 6

      The declaration of an identifier for a variable that has

      block scope that specifies `__thread' shall also specify

      either `extern' or `static'.

      The `__thread' specifier shall be used only with variables.

      5.48.2 ISO/IEC 14882:1998 Edits for Thread-Local Storage

      ---

      The following are a set of changes to ISO/IEC 14882:1998 (aka C++98)

      that document the exact semantics of the language extension.

      * [intro.execution]

      New text after paragraph 4

      A "thread" is a flow of control within the abstract machine.

      It is implementation defined whether or not there may be more

      than one thread.

      New text after paragraph 7

      It is unspecified whether additional action must be taken to

      ensure when and whether side effects are visible to other

      threads.

      * [lex.key]

      Add `__thread'.

      * [basic.start.main]

      Add after paragraph 5

      The thread that begins execution at the `main' function is

      called the "main thread". It is implementation defined how

      functions beginning threads other than the main thread are

      designated or typed. A function so designated, as well as

      the `main' function, is called a "thread startup function".

      It is implementation defined what happens if a thread startup

      function returns. It is implementation defined what happens

      to other threads when any thread calls `exit'.

      * [basic.start.init]

      Add after paragraph 4

      The storage for an object of thread storage duration shall be

      statically initialized before the first statement of the

      thread startup function. An object of thread storage

      duration shall not require dynamic initialization.

      * [basic.start.term]

      Add after paragraph 3

      The type of an object with thread storage duration shall not

      have a non-trivial destructor, nor shall it be an array type

      whose elements (directly or indirectly) have non-trivial

      destructors.

      * [basic.stc]

      Add "thread storage duration" to the list in paragraph 1.

      Change paragraph 2

      Thread, static, and automatic storage durations are

      associated with objects introduced by declarations [...].

      Add `__thread' to the list of specifiers in paragraph 3.

      * [basic.stc.thread]

      New section before [basic.stc.static]

      The keyword `__thread' applied to a non-local object gives the

      object thread storage duration.

      A local variable or class data member declared both `static'

      and `__thread' gives the variable or member thread storage

      duration.

      * [basic.stc.static]

      Change paragraph 1

      All objects which have neither thread storage duration,

      dynamic storage duration nor are local [...].

      * [dcl.stc]

      Add `__thread' to the list in paragraph 1.

      Change paragraph 1

      With the exception of `__thread', at most one

      STORAGE-CLASS-SPECIFIER shall appear in a given

      DECL-SPECIFIER-SEQ. The `__thread' specifier may be used

      alone, or immediately following the `extern' or `static'

      specifiers. [...]

      Add after paragraph 5

      The `__thread' specifier can be applied only to the names of

      objects and to anonymous unions.

      * [class.mem]

      Add after paragraph 6

      Non-`static' members shall not be `__thread'.

      Roger Leigh

      Printing on GNU/Linux? http://gimp-print.sourceforge.net/

      GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.

      #5; Tue, 29 Apr 2008 19:47:00 GMT