Beispiel für die Verwendung von aufrüstbaren Mutexen für Boost

Ich habe eine Multithread-Serveranwendung, die Mutex-Sperren für einen gemeinsam genutzten Speicher benötigt.

The geteilter Speicher sind im Allgemeinen sTL Diagramme etc.

Die meiste Zeit lese ich nur von der Karte. Aber ich muss es auch gelegentlich ergänzen.

z.B. typedef std :: map MessageMap; MessageMap msgmap; boost: shared_mutex access _;

void ProcessMessage(Message* message)
{
  //  Access message... read some stuff from it  message->...

  UUID id = message->GetSessionID();

  // Need to obtain a lock here. (shared lock? multiple readers)
  // How is that done?
  boost::interprocess::scoped_lock(access_);

  // Do some readonly stuff with msgmap
  MessageMap::iterator it = msgmap.find();
  // 

  // Do some stuff...

  // Ok, after all that I decide that I need to add an entry to the map.
  // how do I upgrade the shared lock that I currently have?
  boost::interprocess::upgradable_lock


  // And then later forcibly release the upgrade lock or upgrade and shared lock if I'm not looking
  // at the map anymore.
  // I like the idea of using scoped lock in case an exception is thrown, I am sure that
  // all locks are released.
}

EDIT: Ich könnte die verschiedenen Sperrtypen verwechseln.

Was ist der Unterschied zwischen Shared / Upgrade und Exclusive. Das heißt, ich verstehe die Erklärung nicht. Es hört sich so an, als ob Sie nur vielen Lesern Zugriff gewähren möchten. Sie möchten lediglich einen gemeinsamen Zugriff erhalten. Und um in Ihren Shared Memory zu schreiben, benötigen Sie lediglich einen Upgrade-Zugang. Oder brauchen Sie exklusiv? Die Erklärung in Boost ist alles andere als klar.

Ist Upgrade-Zugriff erhalten, weil Sie schreiben könnten. Aber geteilt bedeutet, dass du definitiv nicht schreiben wirst, heißt das?

EDIT: Lass mich erklären, was ich mit ein bisschen mehr Klarheit machen möchte. Mit den Antworten bin ich noch nicht zufrieden.

Hier ist das Beispiel noch einmal, aber mit einem Beispiel für Code, den ich auch benutze. Nur eine Illustration, nicht der eigentliche Code.

typedef boost::shared_mutex Mutex;
typedef boost::shared_lock<Mutex> ReadLock;
typedef boost::unique_lock<Mutex> WriteLock;
Mutex mutex;
typedef map<int, int> MapType;    // Your map type may vary, just change the typedef
MapType mymap;

void threadoolthread() // There could be 10 of these.
{   
    // Add elements to map here
    int k = 4;   // assume we're searching for keys equal to 4
    int v = 0;   // assume we want the value 0 associated with the key of 4

    ReadLock read(mutex); // Is this correct?
    MapType::iterator lb = mymap.lower_bound(k);
    if(lb != mymap.end() && !(mymap.key_comp()(k, lb->first)))
    {
        // key already exists
    }
    else
    {
        // Acquire an upgrade lock yes?  How do I upgrade the shared lock that I already        have?
        // I think then sounds like I need to upgrade the upgrade lock to exclusive is that correct as well?

        // Assuming I've got the exclusive lock, no other thread in the thread pool will be able to insert.
        // the key does not exist in the map
        // add it to the map
        {
          WriteLock write(mutex, boost::adopt_lock_t());  // Is this also correct?
          mymap.insert(lb, MapType::value_type(k, v));    // Use lb as a hint to insert,
                                                        // so it can avoid another lookup
        }
        // I'm now free to do other things here yes?  what kind of lock do I have here, if any?  does the readlock still exist?
    }

Antworten auf die Frage(4)

Ihre Antwort auf die Frage