Example of how to use boost upgradeable mutexes

后端 未结 2 1915
小蘑菇
小蘑菇 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   
    #include   
    
    int
    main()
    {
        typedef boost::shared_mutex Mutex;
        typedef boost::shared_lock ReadLock;
        typedef boost::unique_lock 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.

提交回复
热议问题