In the main program, the two threads of t1 and t2 have been associated with the adder() and remover() functions. The functions are active if the number of items is greater than zero.
The call to RLock() is carried out inside the __init__ method of the Box class:
class Box:
def __init__(self):
self.lock = threading.RLock()
self.total_items = 0
The two adder() and remover() functions interact with the items of the Box class, respectively, and call the Box class methods of add() and remove().
In each method call, a resource is captured and then released using the lock parameter that is set in the _init_ method.
Here is the output:
N° 16 items to ADD
N° 1 items to REMOVE
ADDED one item -->15 item to ADD
REMOVED one item -->0 item to REMOVE
ADDED one item -->14 item to ADD
ADDED one item -->13 item to ADD
ADDED one item -->12 item to ADD
ADDED one item -->11 item to ADD
ADDED one item -->10 item to ADD
ADDED one item -->9 item to ADD
ADDED one item -->8 item to ADD
ADDED one item -->7 item to ADD
ADDED one item -->6 item to ADD
ADDED one item -->5 item to ADD
ADDED one item -->4 item to ADD
ADDED one item -->3 item to ADD
ADDED one item -->2 item to ADD
ADDED one item -->1 item to ADD
ADDED one item -->0 item to ADD
>>>