views:

306

answers:

5

Does anybody know of (or can post) an example of the use of thread-specific data? I'm looking for something that's clearly explained and easy to understand. I've got a global char * variable that I'm wanting to share between a couple threads, and I'm thinking this is what the thread specific data mechanism in C is for, am I right?

I'm a Linux user!

A: 

This is a good example if you are on the Windows platform

http://msdn.microsoft.com/en-us/library/ms686991(VS.85).aspx

Laserallan
I'm a Linux user actually, but thanks...
Bill
+2  A: 

The errno variable from the original C runtime library is a good example. If a process has two threads making system calls, it would be extremely bad for that to be a shared variable.

thread 1:

int f = open (...);
if (f < 0)
    printf ("error %d encountered\n", errno);

thread 2:

int s = socket (...);
if (s < 0)
    printf ("error %d encountered\n", errno);

Imagine the confusion if open and socket are called at about the same time, both fail somehow, and both try to display the error number!

To solve this, multi-threaded runtime libraries make errno an item of thread-specific data.

wallyk
A: 

The short answer to your question is: you don't have to do anything to share a variable between multiple thread. All the global variables are shared among all threads by default.

When a variable has to be different for each thread, if you are using a ISO-C99 compliant implementation (like GCC), you only need to add the __thread storage class keyword to your variable declaration, as in:

__thread char *variable;

This will instruct all the tiers in the building chain (cc, ld, ld.so, libc.so and libpthread.so) to manipulate this variable in a special thread-specific way.

The following compilers support this syntax (cf wikipedia):

  • Sun Studio C/C++
  • IBM XL C/C++
  • GNU C
  • Intel C/C++ (Linux systems)
  • Borland C++ Builder
Jerome
Do you have a reference for `__thread` being part of the ISO-C99 Standard?
pmg
@pmg: In the gcc documentation, this feature is documented as part of C99 implementation: http://gcc.gnu.org/onlinedocs/gcc/C99-Thread_002dLocal-Edits.html
Jerome
A: 

Actually, thread-specific data is for when you DON'T want to share data between threads -- with thread-specific data, each thread can use the same variable name, but that variable refers to distinct storage.

With gcc, you can declare a variable as thread-specific using the "__thread" attribute. If you are only trying to make a primitive type thread-specific, and you are only dealing with Linux and GCC, then this is a possible solution. If you actually want to be portable, though, between various unices (a desireable goal), or if you want to make complex data types thread-specific, than you need to use the UNIX routines for that...

The way it works in UNIX is that you use phread_key_create before you spawn any threads, in order to create a unique variable name. You then use phread_setspecific and pthread_getspecific to modify/access the data associated with the key. The semantics of the set/get specific functions is that the key behaves as an index into a map, where each thread has its own map, so executing these routines from different threads causes different data to be accessed/modified. If you can use a map, you can use thread-specific storage.

Obviously, when you are done, you need to call the appropriate routines to cleanup the data. You can use pthread_cleanup_push to schedule a cleanup routine to deallocate any datastructures you have associated with the thread-specific key, and you can use pthread_key_destroy when the key is no longer in use.

Michael Aaron Safyan