English 中文(简体)
example of thread specific data in C
原标题:

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!

问题回答

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 pthread_key_create before any thread is spawned, in order to create a unique variable name. You then use pthread_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.

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
", errno);

thread 2:

int s = socket (...);
if (s < 0)
    printf ("error %d encountered
", 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.

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




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签