from threading import Lock
from threading import Thread
class LockedList(list):
def __init__(self, *args, **kwargs):
self._lock = Lock()
super(LockedList, self).__init__(*args, **kwargs)
def remove(self, elem):
with self._lock:
super(LockedList, self).remove(elem)
def insert(self, i, elem):
with self._lock:
super(LockedList, self).insert(i, elem)
def __contains__(self, elem):
with self._lock:
super(LockedList, self).__contains__(elem)
list = [2, 3, 4]
for i in range(100):
t1 = threading.Thread(target=list.insert(i, i))
if i % 2 == 0:
t2 = threading.Thread(target=list.remove(i))
#10 output
for i in range(len(list)):
if i % 10 == 0 and i != 0:
print()
print(list[i], end=' ')
I wrote this code for using locked_list decorator. But i don't know how to test it. Is it right code to test? I want to test list.insert() and list.remove() by using thread whether it causes race condition.