Can thread trying to std::lock[_unique] an std::shared_mutex be starved by threads calling std::lock_shared? Can thread trying to std::lock[_unique] an std::shared_mutex be starved by threads calling std::lock_shared? multithreading multithreading

Can thread trying to std::lock[_unique] an std::shared_mutex be starved by threads calling std::lock_shared?


More or less: would lock[_unique]() on a std::shared_mutex block any attempts to further lock_shared() it?

The standard doesn't specify whether that should happen or not, it only says:

Effects: Blocks the calling thread until shared ownership of the mutex can be obtained for the calling thread.

So it's left up to the implementation whether it makes later readers wait behind pending writers or not.

If the implementation wants to prevent writer starvation then it might need to have a "writer waiting" flag that is set when a thread tries to obtain a unique lock, so that later attempts to get a shared lock will block, waiting behind the unique locker. In the N2406 reference implementation this is the write_entered_ bit in the state member.