Example of how to use boost upgradeable mutexes

后端 未结 2 1905
小蘑菇
小蘑菇 2020-12-14 12:37

I have a multithreaded server application that needs mutex locks over some shared memory.

The shared memory are basically sTL maps etc.

Much of the time I\'m

相关标签:
2条回答
  • 2020-12-14 13:15

    You don't want boost-interprocess if you're just using a single process. As the library name implies, it's used for Inter-Process Communication (IPC). You most likely want to use the boost-thread mutex and locking concepts.

    #include <boost/thread/locks.hpp>  
    #include <boost/thread/shared_mutex.hpp>  
    
    int
    main()
    {
        typedef boost::shared_mutex Mutex;
        typedef boost::shared_lock<Mutex> ReadLock;
        typedef boost::unique_lock<Mutex> WriteLock;
        Mutex mutex;
    
        {
            // acquire read lock
            ReadLock read( mutex );
    
            // do something to read resource
        }
    
        {
            // acquire write lock
            WriteLock write( mutex, boost::adopt_lock_t() );
    
            // do something to write resource
        }
    }
    

    there's a post on the boost mailing list explaining this as well.

    0 讨论(0)
  • 2020-12-14 13:22

    You said that your application is multithreaded, so you should use boost::thread not boost::interprocess.

    From the documentation (not tested) you should do it this way:

    typedef boost::thread::shared_mutex shared_mutex;
    boost::thread::upgrade_lock<shared_mutex> readLock(access_);
    
    // Read access...
    
    boost::thread::upgrade_to_unique_lock<shared_mutex> writeLock(readLock);
    
    // Write access..
    

    Also note that you acquire it while you lock for read access, so someone may delete this node and it's no longer valid when you get to the write section. WRONG, sorry.

    EDIT: I think that explanation in boost is clear. Let's try rephrase it to you anyway:

    There are three main types of mutex concepts (I don't count TimedLockable since it's not related to your question):

    • Lockable — just a simple, exclusive ownership mutex. If someone lock()s it no-one can lock() it again until the owner unlock()s it. boost::thread::mutex implements this concept. To lock this concept in RAII style use lock_guard or unique_lock for a more complex interface.
    • SharedLockable — is a Lockable with an additional "shared" ownership. You can get the exclusive ownership with lock() or the shared ownership with lock_shared(). If you lock the shared part you cannot upgrade your ownership to exclusive one. You need to unlock_shared() and lock() again which means that somebody else may modify the protected resource between unlock_shared() and lock(). It's useful when you know a priori what kind of access to the resource you will do. shared_mutex implements this concept. Use lock_guard or unique_lock to get the exclusive ownership and shared_lock to get the shared ownership.
    • UpgradeLockable — is SharedLockable which allows you to upgrade from shared ownership to exclusive ownership without unlocking. shared_mutex implements this concept too. You can use the above locks to get exclusive or shared ownership. To get an upgradable shared ownership use upgrade_lock and upgrade it with upgrade_to_unique_lock.
    0 讨论(0)
提交回复
热议问题