Is there an 'upgrade_to_unique_lock' for boost::interprocess?

百般思念 提交于 2019-12-05 13:52:10

All Boost.Interprocess upgradable locks support upgrade per this. Definition here.

Regarding your broader question - I would think that this is precisely what you want. Readers can still work concurrently, and you have to prevent concurrent writes. Unless you can partition the shared memory such that more constrained access is guaranteed, this looks the best.

Solution by OP.

The answer, as stated in the question comments is to use the member function unlock_upgradable_and_lock. If there is an boost::interprocess analog to upgrade_to_unique_lock, I don't know where it is. But the writer() function can be rewritten as:

while (1) {
  // Get upgrade lock on the mutex
  myMutex.lock_upgradable();

  // Get exclusive access and block everyone else
  myMutex.unlock_upgradable_and_lock();

  // Write values here

  // Unlock the mutex (and stop blocking readers)
  myMutex.unlock();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!