F
Felix Kater
Hi,
normally I use a local variable assigned to different return values inside the function and return it like this:
[example 1:]
int f(){
int my_err_code;
my_err_code=1;
if( /* ... */ ){
/* result ok */
my_err_code=0;
}
return my_err_code;
}
If I now add two macros to make the function thread save (assume they
work properly) isn't there still a slight chance that the return
variable is changed by a second thread just before the first thread
leaves the function?
[example 2:]
int f(){
int my_err_code;
MACRO_THREADS_ENTER();
my_err_code=1;
if( /* ... */ ){
/* result ok */
my_err_code=0;
}
MACRO_THREADS_LEAVE();
/* (Couldn't the second thread NOW have already changed
my_err_code=1 ? */
return my_err_code;
}
So, I wonder if it is necessary to return constant values to be thread
save in functions and code the fucntion like this:
[example 3:]
int f(){
MACRO_THREADS_ENTER();
if( /* ... */ ){
/* result ok */
goto jmp_ok;
}
MACRO_THREADS_LEAVE();
return 1;
jmp_ok:
MACRO_THREADS_LEAVE();
return 0;
}
normally I use a local variable assigned to different return values inside the function and return it like this:
[example 1:]
int f(){
int my_err_code;
my_err_code=1;
if( /* ... */ ){
/* result ok */
my_err_code=0;
}
return my_err_code;
}
If I now add two macros to make the function thread save (assume they
work properly) isn't there still a slight chance that the return
variable is changed by a second thread just before the first thread
leaves the function?
[example 2:]
int f(){
int my_err_code;
MACRO_THREADS_ENTER();
my_err_code=1;
if( /* ... */ ){
/* result ok */
my_err_code=0;
}
MACRO_THREADS_LEAVE();
/* (Couldn't the second thread NOW have already changed
my_err_code=1 ? */
return my_err_code;
}
So, I wonder if it is necessary to return constant values to be thread
save in functions and code the fucntion like this:
[example 3:]
int f(){
MACRO_THREADS_ENTER();
if( /* ... */ ){
/* result ok */
goto jmp_ok;
}
MACRO_THREADS_LEAVE();
return 1;
jmp_ok:
MACRO_THREADS_LEAVE();
return 0;
}