1

In this talk on concurrency, Arthur O'Dwyer explains the "blue/green" pattern. This looks like something I'd like to have in my toolbox but don't fully grasp it yet.

Essentially it creates two categories for a shared state (namely blue and green) and has:

  • A write side, were modifications are atomically added only when the blue state is consistent:

    using ConfigMap = std::map<std::string, std::string>;
    std::atomic<std::shared_ptr<const ConfigMap>> g_config;
    
    void setDefaultHostname(std::string const& value) {
      std::shared_ptr<const ConfigMap> blue = g_config.load();
      do {
        std::shared_ptr<ConfigMap> green = std::make_shared<ConfigMap>(*blue);
        green->insert_or_assign("default.hostname", value);
      } while (g_config.compare_exchange_strong(blue, std::move(green)));
    }
    
  • A read side that gets the current value in a lock free manner

    std::shared_ptr<const std::string> getDefaultHostname() {
      std::shared_ptr<const ConfigMap> blue = g_config.load();
      auto& value = blue.at("default.hostname");
      return std::shared_ptr<const std::string>(std::move(blue), &value);
    }
    

That said, here are my questions:

  1. Is anyone (open source library/established project) using this in production?
  2. Is there a better explanation available? An alternative example?
  3. Is it worth the trouble? Especially since the write side has to repeat the expensive clone operation until the blue state contains solely our modification.
Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
  • you can also see the example at https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange – apple apple Nov 07 '21 at 12:58
  • and https://en.wikipedia.org/wiki/Compare-and-swap – apple apple Nov 07 '21 at 13:01
  • Possible duplicate: [Do lock-free algorithms really perform better than their lock-full counterparts?](https://stackoverflow.com/questions/5680869/do-lock-free-algorithms-really-perform-better-than-their-lock-full-counterparts) – apple apple Nov 07 '21 at 13:02
  • Possible better fit duplicate https://stackoverflow.com/questions/1585818/when-are-lock-free-data-structures-less-performant-than-mutual-exclusion-mutexe/4044614 – apple apple Nov 07 '21 at 13:11
  • @appleapple Thank you for the links but please don't reduce the question to "what is compare exchange". I'm aware of atomic operations, what I'm asking is whether the blue/green pattern [sth well known in devops](https://docs.cloudfoundry.org/devguide/deploy-apps/blue-green.html) has uses in C++, some insights on the implementation (like how am I even able to use atomics with smart pointers, did this addition allow us more cool patterns etc) and whether you've come across it in other projects. Ultimately if you view its naming as a marketing stunt, I'd also like to know about it. – Lorah Attkins Nov 07 '21 at 13:34
  • @SolomonSlow StackOverflow is not debugging oriented. There was even a question on meta on whether to [close debugging questions](https://meta.stackexchange.com/questions/186402/stack-overflow-a-debugging-platform) mentioning that "A lot of the current questions **from new users** on Stack Overflow ask for debugging". The primary purpose of the Stack Exchange platform is to collect useful information that will be valuable to others. If this sounds unreasonable check how many debugging questions exist in the most upvoted topics https://stackoverflow.com/questions/tagged/c%2b%2b?tab=Votes – Lorah Attkins Nov 07 '21 at 15:01
  • @LorahAttkins afaict it's the same as compare and swap (where the target type is `const ConfigMap*` instead of say, `int`) – apple apple Nov 07 '21 at 15:37
  • (I compare it to `int` because that algorithm, by use immutable type, replacing the whole object stored inside `atomic` (instead update the state of that object.)) – apple apple Nov 07 '21 at 15:43
  • [A common approach to implementing a lock-free data structure is to have a mutable reference to an immutable object ... If the CompareExchange works, great. If not, ditch the new object, re-grab the reference, and start over.](https://stackoverflow.com/a/4044614/5980430) This specific answer probably answer the pros and cons. – apple apple Nov 07 '21 at 15:49
  • note: if two pointer are equal, their point-to object are be consider equal here. – apple apple Nov 07 '21 at 15:56

0 Answers0