From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,ef6840dca498bf03 X-Google-Attributes: gidfee84,public From: Dave Butenhof Subject: Re: Thread Local Storage Date: 1997/06/16 Message-ID: <33A52D08.1CFB@zko.dec.com>#1/1 X-Deja-AN: 248814836 References: <33A1D58A.25A1@us.gases.boc.com> Organization: Digital Equipment Corporation Newsgroups: comp.unix.osf.osf1 Mike Parker wrote: > > Hey there! > > We're doing some multi-threaded programming on DEC Unix 4.0b using the > DEC C++ compiler (v5.5-004) and have a question about static thread > storage. > > How do we define a variable that is static within the thread, i.e., it is > created when the thread is created and is destroyed when the thread goes > away. > > I think the solution should be provided by the compiler. Something like > the extended storage attribute 'thread' in Microsoft c++. First, beware that "thread static storage" (usually known as TLS, for "thread local storage") is an illusion. It's not static, and it's far from free. POSIX threads provides "thread specific data" (TSD), and TLS is mostly an expensive layer of compiler "syntactic sugar" on top of TSD. Yes, it has to be supported by the compiler, and current versions of the compilers don't. Furthermore, extensions to the POSIX TSD model are required to do TLS correctly, and 4.0b doesn't support those extensions -- there's nothing a compiler can do about this. (I've written the necessary extensions, and they're in a release stream, but it wouldn't be appropriate for me to make any announcements about when those extensions might ship, much less about compiler or development tool support.) In terms of efficiency, TLS can be nice if you have a situation where all (or nearly all) of the threads in a process will frequently access per-thread data. Because TLS is created automatically at thread startup, it allows you to use the data without checking whether you need to create it. If most of your threads frequently use the same per-thread data, that can be a substantial savings. If most of your threads don't use a TLS value, then TLS can be expensive because the threads still have to allocate and initialize per-thread copies of the data at startup, and then destroy that (unused) copy at termination. In that case you're far better off using the POSIX TSD interfaces directly, creating per-thread copies dynamically only in the threads that need it. /---------------------------[ Dave Butenhof ]--------------------------\ | Digital Equipment Corporation 110 Spit Brook Rd ZKO2-3/Q18 | | 603.881.2218, FAX 603.881.0120 Nashua NH 03062-2698 | | butenhof@zko.dec.com http://members.aol.com/drbutenhof | \-----------------[ Better Living Through Concurrency ]----------------/