Understanding a race condition

The 9.01_race_condition.py code demonstrates a race condition. The program is as follows:

import threadingclass RaceConditionDemo:  def __init__(self):    self.shared_var = 0    self.total_count = 100000    self.demo_of_race_condition()  def increment(self):    for i in range(self.total_count):      self.shared_var += 1  def decrement(self):    for i in range(self.total_count):      self.shared_var -= 1  def demo_of_race_condition(self):    t1 = threading.Thread(target=self.increment)    t2 = threading.Thread(target=self.decrement)    t1.start()    t2.start()    t1.join()    t2.join()    print("value of shared_var after all increments & decrements :", self.shared_var)if __name__ == "__main__":  for i in range(100):     RaceConditionDemo()

The preceding code consists ...

Get Tkinter GUI Application Development Blueprints - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.