No, it should not be global. You can create it in a function and then pass it to your threads as an argument as follows:
i = 0
def do_work(lock):
global i
while i < len(my_list):
with lock:
my_i = i
i += 1
my_list[my_i].some_works()
def main():
lock = threading.RLock()
workers = [threading.Thread(target=do_work, args=lock,) for _ in range(8)]
for worker in workers:
worker.start()
main()
source
share