I am creating my own IOsystem, which is using fstream. There will be many threads and my question is how can I keep it safe which means to allow multiple read from one file but keep writing exclusive. So for example if there are two threads that want to write to file I want the second one to wait until first one close its fstream. Here Accessing a single file with multiple threads it was proposed to use TMultiReadExclusiveWriteSynchronizer but it am not sure if it's the best option. One my idea is to just keep the map and check it manually before opening any file if the file can be safely open, if not make the thread wait until the file is released.
EDIT: And is there any way to open fstream in exclusive mode? If You think that fstream is the worst option use in many threads enviroments what are other possibilities ?