I’m using C++, and I’m using the boost library mutex, recursive_mutex and other synchronization objects.
I often have the following pattern:
- void RebuildAll(). This function makes many internal changes.
- void DoSomethingA(). Do some work using what RebuildAll built.
- void DoSomethingB(). Do some work using what RebuildAll built.
- void DoSomethingC(). Do some work using what RebuildAll built.
- …
The functions can be called from different threads. I want to be able to execute DoSomethingA(), DoSomethingB() and DoSomethingC() in parallel. But when RebuildAll() is called, I need to make sure that the DoSomething functions are not being executed.
Is there anything to help me protect these functions?
This is often called a reader-writer lock. The rules for a reader-writer lock are:
In your example,
RebuildAll()would be a writer andDoSomethingA()throughDoSomethingC()would be readers.Boost has an implementation of a reader-writer lock called
boost::shared_mutex. This is not yet in the standard library, though.