I am taking an operating systems course. I would like to ask about Locks and critical section. Consider the following C
function that can be called by multiple threads. I use a lock whenever the shared resource balance
is used.
int withdraw(account, amount)
{
acquire(lock);
balance = get_balance(account);
balance = balance - amount;
put_balance(account, balance);
release(lock);
return balance;
}
My question is should I do it like that or should I release the lock after the return
statement? What are the consequences of either way and which of them is a better practice?
I saw another similar Question but it I couldn't understand from it what is the proper thing to do in my example case.