Question: Solving the Race Condition Issue using Python Multithreading Scenario: Suppose you are simulating with a shared bank account accessed by two users ( threads )

Solving the Race Condition Issue using Python Multithreading
Scenario:
Suppose you are simulating with a shared bank account accessed by two users (threads).
Both users can deposit and withdraw money from the account concurrently. Without
proper synchronization, the balance of the account might become inconsistent due to a
race condition. Identify the race condition in this situation and resolve it using one of the
available synchronous locking techniques.
Instructions:
Problem Simulation [3 Marks]: Write a Python program that simulates two
threads, each performing a series of deposit and withdrawal operations on a
shared bank account object. You must show how a race condition can occur when
multiple threads access the shared balance simultaneously without any
synchronization.
Synchronization [3 Marks]: After identifying the race condition, modify the
program to solve this issue by applying one of the synchronous locking
mechanisms, such as (e.g., Lock, RLock, Semaphore, etc.)
Code Requirements [2 Marks]:
The shared bank account class should have methods for depositing and
withdrawing money.
Two threads should perform multiple deposit and withdrawal operations.
Before synchronization, demonstrate how the race condition causes
incorrect balances.
After synchronization, ensure the bank account balance remains consistent.
Report [2 Marks]: Along with the code, submit a short report explaining:
What a race condition is and how it occurred in your initial code.
Which locking technique you used to solve the issue and why you chose it.
How the race condition was resolved with synchronization.
Solving the Race Condition Issue using Python

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!