Question: ( 2 0 points ) The following multi - threaded code snippets are supposed to add amount to a global variable balance . For each

(20 points) The following multi-threaded code snippets are supposed to add amount to a global variable balance. For each of the snippets, determine if race conditions can occur. If there is a race condition, use a scenario of two threads A and B executing the same code to show an example of a race condition. Also, lets assume the operations associated with a code statement are atomic. Example: void deposit (int amount){ int tmp = balance; // line 1 tmp = tmp + amount; // line 2 balance = tmp; // line 3} Answer: Race condition. For example, when A1 B1 A2 A3 B2 B3(A1 means thread A executing line 1.)(1) void deposit (int amount){ int tmp = amount; // line 1 lock(); // line 2 tmp = tmp + balance;// line 3 unlock(); // line 4 balance = tmp; // line 5}(2) void deposit (int amount){ int tmp = amount; // line 1 lock(); // line 2 tmp = tmp + balance;// line 3 balance = tmp; // line 4 unlock(); // line 5}(3) void deposit (int amount){ int *tmp = &balance; // line 1 lock(); // line 2*tmp =*tmp + amount;// line 3 unlock(); // line 4 balance =*tmp; // line 5}(4) void deposit (int amount){ int *tmp = &amount; // line 1 lock(); // line 2*tmp =*tmp + balance; // line 3 unlock(); // line 4 balance =*tmp; // line 5}

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 Programming Questions!