...
| Code Block | ||
|---|---|---|
| ||
#include <pthread.h>
#include <stdio.h>
#include<malloc.h>
/* function prototypes */
void destructor(void * data);
void* function();
void add_data();
void print_data();
/* global key to the thread specific data */
pthread_key_t key;
enum {max_threads = 3};
int main( void )
{
   int i,ret;
   pthread_t thread_id[max_threads];
   /* create the key before creating the threads */
   pthread_key_create( &key, NULL );
   /* create threads that would store specific data*/
   for(i =0; i < max_threads; i++){
       pthread_create( &thread_id[i], NULL, function, NULL );
   }
   for(i=0;i < max_threads; i++){
       ret = pthread_join(thread_id[i], NULL);
if(ret !=0){
/*Handle Error */
}
   }
   pthread_key_delete(key);
}
void *function(void)
{
   add_data();
   print_data();
   pthread_exit(NULL);
}
void add_data(void)
{
   int *data = get_data();
   pthread_setspecific( key,(void *)data);
}
int *get_data()
{
   int *arr = malloc(2*sizeof(int));
   *arr = rand();
   *(arr+1) = rand();
   return arr;
}
void print_data()
{
   /* get this thread's global data from key */
   int *data = pthread_getspecific(key);
/* Print data */
}
|
...
| Code Block | ||
|---|---|---|
| ||
#include <pthread.h>
#include <stdio.h>
#include<malloc.h>
/* function prototypes */
void destructor(void * data);
void* function();
void add_data();
void print_data();
/* global key to the thread specific data */
pthread_key_t key;
enum {max_threads = 3};
int main( void )
{
   int i,ret;
   pthread_t thread_id[max_threads];
   /* create the key before creating the threads */
   pthread_key_create( &key, NULL );
   /* create threads that would store specific data*/
   for(i =0; i < max_threads; i++){
       pthread_create( &thread_id[i], NULL, function, NULL );
   }
   for(i=0;i < max_threads; i++){
        ret = pthread_join(thread_id[i], NULL);
if(ret !=0){
/*Handle Error */
}
   }
   pthread_key_delete(key);
}
void *function(void)
{
   add_data();
   print_data();
free(pthread_getspecific(key));
   pthread_exit(NULL);
}
void add_data(void)
{
   int *data = get_data();
   pthread_setspecific( key,(void *)data);
}
int *get_data(void)
{
   int *arr = malloc(2*sizeof(int));
   *arr = rand();
   *(arr+1) = rand();
   return arr;
}
void print_data(void)
{
   /* get this thread's global data from key */
   int *data = pthread_getspecific(key);
/* Print data */
}
|
...
| Code Block | ||
|---|---|---|
| ||
#include <pthread.h>
#include <stdio.h>
#include<malloc.h>
/* function prototypes */
void destructor(void * data);
void* function();
void add_data();
void print_data();
/* global key to the thread specific data */
pthread_key_t key;
enum {max_threads = 3};
void destructor(void * data){
   pthread_setspecific(key, NULL); // thread specific should be cleared (could be sensitive)
   free(data);
}
int main( void )
{
   int i;
   pthread_t thread_id[max_threads];
   /* create the thread specific data key before creating the threads */
   pthread_key_create( &key, destructor );
   /* create thread that will use the key */
   for(i =0; i < max_threads; i++){
       pthread_create( &thread_id[i], NULL, function, NULL );
   }
Â
   for(i=0;i < max_threads; i++){
       int ret = pthread_join(thread_id[i], NULL);
       if(ret !=0){
           /* Handle Error */
       }
   }
   pthread_key_delete(key);
}
void *function(void)
{
   add_data();
   print_data();
   pthread_exit(NULL);
}
void add_data(void)
{
   int *data = get_data();
/* set current thread's data */
   pthread_setspecific( key,(void *) data);
}
int *get_data(void){
   int *arr = malloc(2*sizeof(int)); /* Not handling the integer overflow */
   *arr = rand();
   *(arr+1) = rand();
   return arr;
}
void print_data(void)
{
   /* retrieve current thread's data from key */
   int *data = pthread_getspecific(key);
/* Print Data */
}
|
...