I have this simple python code:
from threading import Lock, Condition, Thread
n_threads = 5
global_list = [[]]*n_threads
def update_list(t_id, lock):
global global_list
for i in range(t_id, t_id+4, 1):
lock.acquire()
global_list[t_id].append(i)
lock.release()
print('finishing', t_id)
all_threads = []
l = Lock()
for i in range(n_threads):
c = Thread(name='name %s' % i, target=update_list, args=(i,l,))
all_threads.append(c)
c.start()
for my_thread in all_threads:
my_thread.join()
Basically what I am trying to do is that each thread updates one of the lists and by the end of my code each list inside of the variable global_list will have elments from a single thread, like this:
global_list[0] = [0,1,2,3]
global_list[1] = [1,2,3,4]
...
Instead what I get is :
global_list[0] = [0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6, 4, 5, 6, 7]
global_list[1] = [0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6, 4, 5, 6, 7]
...
I am using a mutex to protect the operation, so basically I am clueless about why it wont work for lists in python and if threre is something I can use to achieve this.
question from:
https://stackoverflow.com/questions/65905005/update-python-list-of-lists-on-multithreaded