Friday, April 26, 2013

pthread_setspecific example c c++


NAME

pthread_getspecific, pthread_setspecific - thread-specific data management

SYNOPSIS

[THR] [Option Start] #include <pthread.h>

void *pthread_getspecific(pthread_key_t
 key);
int pthread_setspecific(pthread_key_t
 key, const void *value); [Option End]

DESCRIPTION

The pthread_getspecific() function shall return the value currently bound to the specified key on behalf of the calling thread.
The pthread_setspecific() function shall associate a thread-specific 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_getspecific() or pthread_setspecific() with a key value not obtained from pthread_key_create() or after key has been deleted with pthread_key_delete() is undefined.
Both pthread_getspecific() and pthread_setspecific() may be called from a thread-specific data destructor function. A call topthread_getspecific() for the thread-specific data key being destroyed shall return the value NULL, unless the value is changed (after the destructor starts) by a call to pthread_setspecific(). Calling pthread_setspecific() from a thread-specific data destructor routine may result either in lost storage (after at least PTHREAD_DESTRUCTOR_ITERATIONS attempts at destruction) or in an infinite loop.
Both functions may be implemented as macros.

RETURN VALUE

The pthread_getspecific() function shall return the thread-specific data value associated with the given key. If no thread-specific data value is associated with key, then the value NULL shall be returned.
If successful, the pthread_setspecific() function shall return zero; otherwise, an error number shall be returned to indicate the error.

ERRORS

No errors are returned from pthread_getspecific().
The pthread_setspecific() function shall fail if:
[ENOMEM]
Insufficient memory exists to associate the non-NULL value with the key.
The pthread_setspecific() function may fail if:
[EINVAL]
The key value is invalid.
These functions shall not return an error code of [EINTR].
Example of (pthread_getspecific, pthread_setspecific)
 Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
#define _MULTI_THREADED
#include <pthread.h>
#include <stdio.h>
#include "check.h"

#define         NUMTHREADS     3
pthread_key_t   tlsKey = 0;

void globalDestructor(void *value)
{
  printf("In the globalDestructor\n");
  free(value);
  pthread_setspecific(tlsKey, NULL);
}


void showGlobal(void)
{
  void                 *global;
  pthread_id_np_t       tid;

  global  = pthread_getspecific(tlsKey);
  pthread_getunique_np((pthread_t *)global, &tid);
  printf("showGlobal: global data stored for thread 0x%.8x%.8x\n",
         tid);
}

void *threadfunc(void *parm)
{
  int           rc;
  int          *myThreadDataStructure;
  pthread_t     me = pthread_self();

  printf("Inside secondary thread\n");

  myThreadDataStructure = malloc(sizeof(pthread_t) + sizeof(int) * 10);
  memcpy(myThreadDataStructure, &me, sizeof(pthread_t));
  pthread_setspecific(tlsKey, myThreadDataStructure);
  showGlobal();
  pthread_exit(NULL);
}

int main(int argc, char **argv)
{
  pthread_t             thread[NUMTHREADS];
  int                   rc=0;
  int                   i=0;

  printf("Enter Testcase - %s\n", argv[0]);

  printf("Create a thread local storage key\n");
  rc = pthread_key_create(&tlsKey, globalDestructor);
  checkResults("pthread_key_create()\n", rc);
  /* The key can now be used from all threads */

  printf("Create %d threads using joinable attributes\n",
         NUMTHREADS);
  for (i=0; i<NUMTHREADS; ++i) {
    rc = pthread_create(&thread[i], NULL, threadfunc, NULL);
    checkResults("pthread_create()\n", rc);
  }

  printf("Join to threads\n");
  for (i=0; i<NUMTHREADS; ++i) {
    rc = pthread_join(thread[i], NULL);
    checkResults("pthread_join()\n", rc);
  }

  printf("Delete a thread local storage key\n");
  rc = pthread_key_delete(tlsKey);
  checkResults("pthread_key_delete()\n", rc);
  /* The key and any remaining values are now gone. */
  printf("Main completed\n");
  return 0;
}

Output:

Enter Testcase - QP0WTEST/TPGETS0
Create a thread local storage key
Create 3 threads using joinable attributes
Join to threads
Inside secondary thread
showGlobal: global data stored for thread 0x000000000000000b
In the globalDestructor
Inside secondary thread
showGlobal: global data stored for thread 0x000000000000000d
In the globalDestructor
Inside secondary thread
showGlobal: global data stored for thread 0x000000000000000c
In the globalDestructor
Delete a thread local storage key
Main completed