I have a List<T>
that multiple threads read from. Rarely some threads add or delete to that list. I know threads must use that object in a synchronized block. But in this way every thread should wait when another one was reading the list.
I want to block only writing threads. So multiple threads can read from the list at the same time, but if another thread wants to edit the list it will wait until every other reading threads are done with the list and every reading thread will wait if a thread is editing the list. Is this possible?
CodePudding user response:
You could implement a Readers-Writer Lock, probably with write-preferring priority:
Readers–writer lock: https://en.wikipedia.org/wiki/Readers–writer_lock
There are some alternatives mentioned on that page too.