I currently program a program which uses multiple threads. To be able to share data across these threads I use Locks like threading.Lock to avoid access problem during run.
But the problem that I got is that I have to create huge amount locks for every data just for reading that data. Even if I ‘group’ data and use for them the same lock there are too many of them.
So I am asking myself: If a thread just reads data and does not changes it (that also applies to all other threads), are there any problems which can occur?
Thanks in advance,
If all your threads just read data, there is no problem accessing the data simultaneously from different threads, as long as reading the data does not change it. Changing the read data can occur in some data structure,
defaultdictbeing a tricky example.If you have a consumer\producer scenario, consider using
Queue.Queue, a thread-safe data structure which allows simultaneous reads and writes. From the Python documentation: