Question: You have been asked to write the withdraw() function for a bankAccount object. typedef struct { int balance; } bankAccount; pthread_mutex_t m; void deposit(int amount)
You have been asked to write the withdraw() function for a bankAccount object.
typedef struct {
int balance;
} bankAccount;
pthread_mutex_t m;
void deposit(int amount)
{
m.lock();
balance += amount;
m.unlock;
}
int getBalance() {
return balance;
}
Which of these locking options will ensure that it works correctly?
A. Lock mutex m at the beginning of withdraw() function, and unlock it at the end.
B. Theres no need to use a lock here, because the value of the balance is being decreased instead of increase.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
