Consider the following situation:
I have a foo object that is used by multiple threads that may or may not repeatedly call the method bar () on foo.
It is perfectly fine (and desirable) that bar () is executed several times in parallel, since it never changes the state of foo.
The problem arises when I need to change the state of foo from the outside (from another thread, and not from one of the "worker" threads) - how can I block foo so that the calling thread blocks until the last worker thread is executed using bar (), and will all worker threads be blocked in bar () until I return foo again?
Obviously, I can't just use a mutex that stays locked while bar () is running, because then I will not have concurrency.
Any ideas? Or is there a better design for these problems?
source share