Multiple-readers, single-writer locks in Boost

前端 未结 3 590
温柔的废话
温柔的废话 2020-12-05 19:50

I\'m trying to implement the following code in a multithreading scenario:

Get shared access to mutex
Read data structure
If necessary:
   Get exclusive acces         


        
3条回答
  •  醉酒成梦
    2020-12-05 20:22

    boost::shared_lock doesn't help in this situation (multiple readers that can become writers), since only a single thread may own an upgradable lock. This is both implied by the quote from the documentation in the question, and by looking at the code (thread\win32\shared_mutex.hpp). If a thread tries to acquire an upgradable lock while another thread holds one, it will wait for the other thread.

    I settled on using a regular lock for all reader/writers, which is OK in my case since the critical section is short.

提交回复
热议问题