The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem with pthreads sery0ga High Level Programming 3 02-21-2007 10:40 AM
pthreads zackz HP-UX 3 01-24-2007 02:03 AM
PThreads justgotthis High Level Programming 0 10-08-2005 03:01 PM
pthreads Esaia High Level Programming 2 06-04-2003 06:32 PM
pthreads fishman2001 High Level Programming 6 06-25-2002 05:01 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-06-2002
S.P.Prasad S.P.Prasad is offline
Registered User
  
 

Join Date: Jan 2002
Location: India
Posts: 111
PThreads

Can anyone explain me how to use pthread_key_create() , pthread_setspecific(), pthread_getspecific() and pthread_key_delete () routines in pthreads.

Kindly state by an example.
  #2 (permalink)  
Old 06-06-2002
killerserv's Avatar
killerserv killerserv is offline Forum Advisor  
Unix Predator
  
 

Join Date: Dec 2000
Location: Phoenix, AZ
Posts: 670
pthread_key_create, creates a thread-specific data key.

#include<pthread.h>
int pthread_key_create ( key, destructor )
pthread_key_t * key;
void (*destructor) (void *);

- The pthread_key_create creates a thread-specific data key. The key is
shared among all threads within the process, but each thread has specific data
associated with the key. The thread-specific data is a void pointer, initially
set to NULL.

#include <pthread.h>
int pthread_setspecific(pthread_key_t key, const void *value);

- The pthread_setspecific() function associates a thread-specific data value with a key obtained via a previous call to pthread_key_create(). Different threads may bind different values to the same key. These values are typically pointers to blocks of dynamically allocated memory that have been reserved for use by the calling thread.

The effect of calling pthread_setspecific() with a key value not obtained from pthread_key_create() or after the key has been deleted with pthread_key_delete() is undefined.

#include <pthread.h>
int pthread_key_delete(pthread_key_t key);

- The pthread_key_delete() function deletes a thread-specific data key previously returned by pthread_key_create(). The thread-specific data values associated with the key need not be NULL at the time pthread_key_delete() is called. It is the responsibility of the application to free any application storage or perform any cleanup actions for data structures related to the deleted key or associated thread-specific data in any threads; this cleanup can be done either before or after pthread_key_delete(). Any attempt to use the key following the call to pthread_key_delete() results in undefined behavior.

pthread_key_delete() can be called from within destructor functions. No destructor functions are invoked by pthread_key_delete(). Any destructor function that may have been associated with the key is no longer called on thread exit.

#include <pthread.h>
void *pthread_getspecific(pthread_key_t key);

- The pthread_getspecific() function returns the value currently associated with the specified thread-specific data key. The effect of calling pthread_getspecific() with a key value not obtained from pthread_key_create() or after the key has been deleted with pthread_key_delete() is undefined. pthread_getspecific() may be called from a thread-specific data destructor function.
  #3 (permalink)  
Old 06-06-2002
S.P.Prasad S.P.Prasad is offline
Registered User
  
 

Join Date: Jan 2002
Location: India
Posts: 111
Thanks. But all this I have seen in the man pages. Actually I am unable to implement it in a program.

Here's an idea of how I tried to implement the above concepts:

I created a thread key and 2 threads in main and made the main waited untill all the threads were over. In one of the thread ( I assumed that it wil be called first) I binded a value to the key using pthread_setspecific. In the other thread I tried to restore the binded value to the key by calling pthread_getspecific routine. But the value returned by the later is a NULL value.

Kindly direct me as to where I am wrong. Kindly state an example in favour of it's lucid explanation and its implementation techniques.
  #4 (permalink)  
Old 06-06-2002
S.P.Prasad S.P.Prasad is offline
Registered User
  
 

Join Date: Jan 2002
Location: India
Posts: 111
Well I found out the problem myself. I should have used pthread_getspecific in the same thread instead of the other. But still I have one doubt - In the thread there is a cost overhead of calling set and get specific at intervals. If we declare static array variables of a type ( as many as there are threads ) , perhaps we can achive the same goal.
Is it safe enough to implement the concept in a medium scale project ?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0