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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!