T
tntelle
I have the following code:
snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);
FILE * f_2 = popen(VARIABLES_2, "r");
size_t r_2;
while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
RETURN_CODE_2[r_2-1] = '\0';
}
pclose(f_2);
but I am worried that this is not thread safe and this is for a multi-
threaded application on AIX 5.3 - compiling with XLC.
I figured i could put a mutex around it, but having not done that
before wasnt 100% sure if i have this correct. Here is what another
friend and I came up with:
if(pthread_mutex_init(&mutex, NULL)){
printf("Unable to init a mutex\n");
return -1;
}
pthread_mutex_lock(&mutex);
snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);
FILE * f_2 = popen(VARIABLES_2, "r");
size_t r_2;
while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
RETURN_CODE_2[r_2-1] = '\0';
}
pclose(f_2);
pthread_mutex_unlock(&mutex);
I would appreciate any comments on this or suggestions for making that
code better... or work =-)
Thank you in advance!
snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);
FILE * f_2 = popen(VARIABLES_2, "r");
size_t r_2;
while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
RETURN_CODE_2[r_2-1] = '\0';
}
pclose(f_2);
but I am worried that this is not thread safe and this is for a multi-
threaded application on AIX 5.3 - compiling with XLC.
I figured i could put a mutex around it, but having not done that
before wasnt 100% sure if i have this correct. Here is what another
friend and I came up with:
if(pthread_mutex_init(&mutex, NULL)){
printf("Unable to init a mutex\n");
return -1;
}
pthread_mutex_lock(&mutex);
snprintf(VARIABLES_2, sizeof VARIABLES_2, "/usr/bin/cksum
'%s'",ARGV1);
FILE * f_2 = popen(VARIABLES_2, "r");
size_t r_2;
while((r_2 = fread(RETURN_CODE_2, sizeof(char), BUFSIZE_2, f_2)) >
1) {
RETURN_CODE_2[r_2-1] = '\0';
}
pclose(f_2);
pthread_mutex_unlock(&mutex);
I would appreciate any comments on this or suggestions for making that
code better... or work =-)
Thank you in advance!