Thread Specific Data
Thread Specific Data means data related to a particular thread,Think of a pool of different data for different threads that each thread can access with a public key...
All things revolves around a key to get the particular data..
what to do--
1.)Create a key
2.)set the specific data for the thread using the key
3.)get the specific data using the key only for a specific thread
Example:suppose there are 4 Threads running and every threads has to create a log file of the task that is performing,so each thread wants a file pointer specific to that thread so we can keep those file pointers as the TSD and can use a key to access the TSD for each thread..
For creating a key use the following function:
int pthread_key_create(pthread_key_t *key, void (*destructor) (void *));
first argument-The key which you have defined...
second argument-The function which you want to run after the completion of the thread,generally for cleanup,descrutor kind of thing.
Note-If you pass a function pointer here, GNU/Linux automatically calls that function when each thread exits, passing the thread-specific value corresponding to that key. This is particularly handy because the cleanup function is called even if the thread is canceled at some arbitrary point in its execution. If the thread-specific value is null, the thread cleanup function is not called. If you don't need a cleanup function, you may pass NULL instead of a function pointer.
For setting the value use the following function:
pthread_setspecific(key, value);
For getting the value use the following function:
pthread_getspecific(key);
No comments:
Post a Comment
Feel free to comment......