1

I have code like this:

arr = [Queue() for _ in range(10)]

And some threads will use the list at the same time. such as arr[0].get(). I'm curious whether it is threadsafe. I know Queue() is threadsafe in python. However, I do not know whether [Queue()] is threadsafe.

mrsiz
  • 193
  • 9
  • 1
    Does this answer your question? [Are lists thread-safe?](https://stackoverflow.com/questions/6319207/are-lists-thread-safe) – mkrieger1 Mar 15 '20 at 02:58
  • It means that it will be threadsafe to modify the same element of the list if the element in the list is threadsafe? – mrsiz Mar 15 '20 at 03:05

1 Answers1

1

Lists are read-safe. As long as no code is changing the size of the list, which would make indexing the list unsafe, you can read it from all of the threads. Since the only objects in the list are thread safe queues, you are good to go.

arr[0].push("foo")
bar = arr[0].pop()

don't change the list itself and are safe.

tdelaney
  • 73,364
  • 6
  • 83
  • 116