Created
December 8, 2016 19:30
-
-
Save mgarg1/1c5d4c22c78ae8d0a8e32288d24a8a6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem is in line 10