Я пытаюсь реализовать следующий код в сценарии многопоточности:
Get shared access to mutex
Read data structure
If necessary:
Get exclusive access to mutex
Update data structure
Release exclusive lock
Release shared lock
Boost threads имеет класс shared_mutex
, который был разработан для модели с несколькими считывателями, с одним писателем. В этом классе есть несколько вопросов о stackoverflow. Однако я не уверен, что это соответствует сценарию выше, где любой читатель может стать писателем. В документации указано:
Концепция UpgradeLockable - это уточнение SharedLockable концепция, позволяющая обновлять владение, а также совместное владение и исключительное право собственности. Это расширение для устройства с несколькими считывателями/ модель с одиночной записью, предоставленная Концепция SharedLockable: одиночныйпоток может иметь обновляемое право собственности в то же время, как другие собственность.
Из слова "одиночный" я подозреваю, что только один поток может содержать обновляемый замок. Остальные имеют только общий замок, который нельзя обновить до эксклюзивной блокировки.
Знаете ли вы, что boost::shared_lock
полезен в этой ситуации (любой читатель может стать писателем), или если есть какой-либо другой способ достичь этого?