I have data protected by a mutex, and accessed by several threads. One runs in the background and updates (write) the data, and other threads do read operations (so they use a shared lock while the first thread uses a unique lock)
Whenever the read threads try to lock the mutex, I would like the writing one to unlock and relock for waiting the reading task completion. The writing thread is a loop, it can periodically check for this.
Is there a standard way to do this or do I need to do my own implementation based on condition variables?
And more specifically, if I own a lock on a mutex, and another thread is blocked trying to lock the mutex, do I have any guarantee that unlocking and relocking immediately will pass the lock to the waiting thread?