So a very simple yet slightly confusing question that i came across on an interview.
Suppose you are in a multithreaded environment, you have some object which can be read and written to. The idea is that many different threads should be able to access your object / collection to read it, but once a thread tries to write to it, all read operations must stop, but if someone is reading from it, others can read from it as well.
What's a good way to design this?
my solution was to have a few threads, all can read this collection lets say but once a thread tries to do a write to it, i would need to notify all other threads that they are no longer allowed to acquire the lock on this object and the threads that are currently holding the lock on the object while reading will have to stop and give up the lock for the write thread which takes priority.
whats a good way to accomplish this?