November 2018
Intermediate to advanced
446 pages
12h 11m
English
The goal of a good lock-based concurrent data structure is to have its locks internally implemented within its class attributes and methods, so that external functions and programs cannot bypass those locks and access a shared concurrent object simultaneously. For our counter data structure, we will be adding an additional attribute for the class, which will hold the lock object that corresponds to the value of the counter. Consider the following new implementation of the data structure in the Chapter16/example2.py file:
# Chapter16/example2.pyimport threadingimport timeclass LockedCounter: def __init__(self): self.value = 0 self.lock = threading.Lock() def increment(self, x): with self.lock: ...
Read now
Unlock full access