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

前端 未结 6 1533
旧时难觅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:12

    Great response by Jim Morris, I stumbled upon this and it took me a while to figure. Here is some simple code that shows that after submitting a "request" for a unique_lock boost (version 1.54) blocks all shared_lock requests. This is very interesting as it seems to me that choosing between unique_lock and upgradeable_lock allows if we want write priority or no priority.

    Also (1) in Jim Morris's post seems to contradict this: Boost shared_lock. Read preferred?

    #include <iostream>
    #include <boost/thread.hpp>
    
    using namespace std;
    
    typedef boost::shared_mutex Lock;
    typedef boost::unique_lock< Lock > UniqueLock;
    typedef boost::shared_lock< Lock > SharedLock;
    
    Lock tempLock;
    
    void main2() {
        cout << "10" << endl;
        UniqueLock lock2(tempLock); // (2) queue for a unique lock
        cout << "11" << endl;
        boost::this_thread::sleep(boost::posix_time::seconds(1));
        lock2.unlock();
    }
    
    void main() {
        cout << "1" << endl;
        SharedLock lock1(tempLock); // (1) aquire a shared lock
        cout << "2" << endl;
        boost::thread tempThread(main2);
        cout << "3" << endl;
        boost::this_thread::sleep(boost::posix_time::seconds(3));
        cout << "4" << endl;
        SharedLock lock3(tempLock); // (3) try getting antoher shared lock, deadlock here
        cout << "5" << endl;
        lock1.unlock();
        lock3.unlock();
    }
    
    0 讨论(0)
  • 2020-11-22 13:22

    Since C++ 17 (VS2015) you can use the standard for read-write locks:

    #include <shared_mutex>
    
    typedef std::shared_mutex Lock;
    typedef std::unique_lock< Lock > WriteLock;
    typedef std::shared_lock< Lock > ReadLock;
    
    Lock myLock;
    
    
    void ReadFunction()
    {
        ReadLock r_lock(myLock);
        //Do reader stuff
    }
    
    void WriteFunction()
    {
         WriteLock w_lock(myLock);
         //Do writer stuff
    }
    

    For older version, you can use boost with the same syntax:

    #include <boost/thread/locks.hpp>
    #include <boost/thread/shared_mutex.hpp>
    
    typedef boost::shared_mutex Lock;
    typedef boost::unique_lock< Lock >  WriteLock;
    typedef boost::shared_lock< Lock >  ReadLock;
    
    0 讨论(0)
  • 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<boost::shared_mutex> lock(_access);
    
      // now we have shared access
    }
    
    void writer()
    {
      // get upgradable access
      boost::upgrade_lock<boost::shared_mutex> lock(_access);
    
      // get exclusive access
      boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
      // now we have exclusive access
    }
    
    0 讨论(0)
  • 2020-11-22 13:31

    1800 INFORMATION is more or less correct, but there are a few issues I wanted to correct.

    boost::shared_mutex _access;
    void reader()
    {
      boost::shared_lock< boost::shared_mutex > lock(_access);
      // do work here, without anyone having exclusive access
    }
    
    void conditional_writer()
    {
      boost::upgrade_lock< boost::shared_mutex > lock(_access);
      // do work here, without anyone having exclusive access
    
      if (something) {
        boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
        // do work here, but now you have exclusive access
      }
    
      // do more work here, without anyone having exclusive access
    }
    
    void unconditional_writer()
    {
      boost::unique_lock< boost::shared_mutex > lock(_access);
      // do work here, with exclusive access
    }
    

    Also Note, unlike a shared_lock, only a single thread can acquire an upgrade_lock at one time, even when it isn't upgraded (which I thought was awkward when I ran into it). So, if all your readers are conditional writers, you need to find another solution.

    0 讨论(0)
  • 2020-11-22 13:32

    Just to add some more empirical info, I have been investigating the whole issue of upgradable locks, and Example for boost shared_mutex (multiple reads/one write)? is a good answer adding the important info that only one thread can have an upgrade_lock even if it is not upgraded, that is important as it means you cannot upgrade from a shared lock to a unique lock without releasing the shared lock first. (This has been discussed elsewhere but the most interesting thread is here http://thread.gmane.org/gmane.comp.lib.boost.devel/214394)

    However I did find an important (undocumented) difference between a thread waiting for an upgrade to a lock (ie needs to wait for all readers to release the shared lock) and a writer lock waiting for the same thing (ie a unique_lock).

    1. The thread that is waiting for a unique_lock on the shared_mutex blocks any new readers coming in, they have to wait for the writers request. This ensures readers do not starve writers (however I believe writers could starve readers).

    2. The thread that is waiting for an upgradeable_lock to upgrade allows other threads to get a shared lock, so this thread could be starved if readers are very frequent.

    This is an important issue to consider, and probably should be documented.

    0 讨论(0)
  • 2020-11-22 13:32

    Use a semaphore with a count that is equal to the number of readers. Let each reader take one count of the semaphore in order to read, that way they can all read at the same time. Then let the writer take ALL the semaphore counts prior to writing. This causes the writer to wait for all reads to finish and then block out reads while writing.

    0 讨论(0)
提交回复
热议问题