I'm working with threads and i have a question regarding using functions with more then one thread.
here are my explanation examples:
example 1, ( what i would like to do. )
Code: Select all
function myfunc( param_1 )
MutexLock myfunc_lock
some_resault += param_1
MutexUnLock myfunc_lock
return some_resault
end function
sub thread1
myfunc( 100 )
end sub
sub thread2
myfunc( 99 )
end sub
example 2, ( what i am doing now to be safe )
Code: Select all
function myfunc( param_1 )
return some_resault + param_1
end function
sub thread1
MutexLock myfunc_lock
myfunc( 100 )
MutexUnLock myfunc_lock
end sub
sub thread2
MutexLock myfunc_lock
myfunc( 99 )
MutexUnLock myfunc_lock
end sub
I have tested example 1 and it seems to not cause errors but im still not convinced its safe as Im not knowledgeable in exactly how functions, parameters and returns work at the lower levels after compiling.
If example 1 is safe then it saves on mutexlocking every function call!
Of course my threads and functions are much more complex then the examples provided but these examples pin point the exact issue at hand.
I have looked up c code and found examples that support example 1 mutexlock use but i would like to be sure.
Regards!
SamL