Example for boost shared_mutex (multiple reads/one write)?

前端 未结 6 1608
旧时难觅i
旧时难觅i 2020-11-22 12:48

I have a multithreaded app that has to read some data often, and occasionally that data is updated. Right now a mutex keeps access to that data safe, but it\'s expensive bec

6条回答
  •  借酒劲吻你
    2020-11-22 13:30

    It looks like you would do something like this:

    boost::shared_mutex _access;
    void reader()
    {
      // get shared access
      boost::shared_lock lock(_access);
    
      // now we have shared access
    }
    
    void writer()
    {
      // get upgradable access
      boost::upgrade_lock lock(_access);
    
      // get exclusive access
      boost::upgrade_to_unique_lock uniqueLock(lock);
      // now we have exclusive access
    }
    

提交回复
热议问题