I have a code block like this:
if(supportsSomeStuff()){
.....
.....
}
else {
.....
.....
}
Now I want to have this block of code in a multiple read, single write lock.
In this case, is it better to lock in the if statement, and release the lock after if, then
lock in the else statement and release it when done?
Or, can I just have one lock for this if-else case and release it when I am done?
If I need to decide on this , what all factors do I need to take into consideration?
Please let me know if you need more information.
Are you considering the following two variants?
vs.
The only (huge) difference between them is that the condition inside
ifstatement is not synchronized in the latter case. Besides (ifsupportsSomeStuff()can be executed in several threads) they are equivalent, although the former case holds the lock for slightly longer time.