Skip to content

Instantly share code, notes, and snippets.

@mgarg1
Created December 8, 2016 19:30
Show Gist options
  • Save mgarg1/1c5d4c22c78ae8d0a8e32288d24a8a6c to your computer and use it in GitHub Desktop.
Save mgarg1/1c5d4c22c78ae8d0a8e32288d24a8a6c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define INIT_VALUE 1.0f
pthread_mutex_t m;
float *getValue(){
static int is_initialized = 0;
static float *actual_data = NULL;
if(!is_initialized){
pthread_mutex_lock(&m);
if(!is_initialized){
actual_data = (float *)malloc(sizeof(float));
*actual_data = INIT_VALUE;
is_initialized = 1;
}
printf("*********** intialised ***************\n");
pthread_mutex_unlock(&m);
}
return actual_data;
}
void *func1(void *t){
printf("%p\n",getValue());
return NULL;
}
int main(){
if(pthread_mutex_init(&m,NULL))
printf("%s\n", "error init mutex");
int i=0;
pthread_t t1[100];
for(i=0;i<100;++i){
if(pthread_create(&t1[i], NULL, func1,NULL))
printf("%s\n", "error creating thread");
}
for(i=0;i<100;++i){
if(pthread_join(t1[i],NULL))
printf("%s\n", "error creating thread");
}
// pthread_t t2;
// pthread_create(&t2, NULL, func1,NULL);
// pthread_join(t2,NULL);
float *temp = getValue();
free(temp);
return 0;
}
@mgarg1
Copy link
Author

mgarg1 commented Dec 8, 2016

Problem is in line 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment