Skip to content

Latest commit

 

History

History

account_balance

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

The Account Balance

The Problem

The primary problem this code addresses is known as a "Race Condition." In concurrent programming, a race condition occurs when two or more threads or goroutines access shared data simultaneously, leading to unpredictable and incorrect results. In this code, multiple goroutines are attempting to modify the bankBalance variable concurrently within a loop. This can lead to several issues, including:

  • Data Corruption: Without proper synchronization, multiple goroutines can read and modify bankBalance simultaneously, resulting in incorrect calculations and potentially corrupting the data.
  • Inconsistent Results: Due to the lack of synchronization, the final bankBalance value may vary with each program execution, leading to inconsistent and unpredictable results.
  • Lost Updates: Simultaneous modifications to bankBalance may lead to some updates being lost, resulting in an inaccurate representation of the actual earnings.
  • Race Conditions: The code may produce race conditions where the order of execution among goroutines affects the final outcome, making it challenging to predict the final bankBalance.

The Solution

The code provides a solution to the synchronization problem by using a Mutex (short for mutual exclusion). A Mutex is a synchronization primitive in Go that allows only one goroutine to access a specific resource at a time, ensuring that concurrent access does not corrupt data or produce unpredictable results.

Heres how the code solves the problem:

  • Mutex Usage: It declares a Mutex variable named balance, which will protect access to the bankBalance variable. The Lock and Unlock methods of the Mutex ensure that only one goroutine can modify bankBalance at a time.
  • Goroutines and Mutex: Inside the loop that iterates through incomes, a goroutine is launched for each income source. Each goroutine locks the balance Mutex before modifying bankBalance and unlocks it after the modification is complete. This ensures that only one goroutine can access bankBalance at a time, preventing race conditions.
  • WaitGroup: The code uses a WaitGroup (wg) to wait for all goroutines to finish before printing the final bankBalance. The wg.Add method increments the WaitGroup counter, and wg.Done is called in each goroutine to decrement the counter when they finish.

By using Mutex and the WaitGroup, the code ensures that concurrent access to bankBalance is synchronized, preventing race conditions and ensuring accurate and predictable results.

Conclusion

This Go code demonstrates a common synchronization problem in concurrent programming and provides a solution using Mutex and WaitGroup to ensure safe access to shared data. Understanding and addressing such synchronization issues is crucial for writing reliable and predictable concurrent programs.