开发者

example of thread specific data in C

开发者 https://www.devze.com 2022-12-11 17:11 出处:网络
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

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 L开发者_如何转开发inux 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\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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消