Here’s the scenario. I have multiple processor threads reading a value. Only 1 thread is allowed to ever write to the value. Due to the setup, nobody can read while a write is in progress, naturally. So, a CCriticalSection would lock out all the worker threads when the 1 guy is writing the new value. However, if I do that, all the worker threads stop each time somebody want’s to look at it (they lock each other out). It creates a bottle-neck in the processing since all the workers have to stop and get in line to read the value. Is there some way, other then CCriticalSection, to allow all the little workers to continue reading away (which is fine, no thread problems there), but only STOP them when the main thread wants to write it?
I’ve considered just setting a boolean value that tells the workers to stop, then Sleep() for a second or so (to let them complete), do the update, then reset the boolean. That just seems..well..heavy handed.
You can use reader/writer locks, http://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock
Which will give you efficient protection