GEODE-7347: Guarding CacheImpl during Cache move to avoid race condition. (#542)

Co-authored-by: Mike Oleske<moleske@pivotal.io>
diff --git a/cppcache/include/geode/Cache.hpp b/cppcache/include/geode/Cache.hpp
index f1f1d6b..3a0635c 100644
--- a/cppcache/include/geode/Cache.hpp
+++ b/cppcache/include/geode/Cache.hpp
@@ -254,7 +254,7 @@
         bool readPdxSerialized,
         const std::shared_ptr<AuthInitialize>& authInitialize);
 
-  std::shared_ptr<CacheImpl> m_cacheImpl;
+  std::unique_ptr<CacheImpl> m_cacheImpl;
 
  protected:
   static bool isPoolInMultiuserMode(std::shared_ptr<Region> regionPtr);
diff --git a/cppcache/src/Cache.cpp b/cppcache/src/Cache.cpp
index e041eb0..af740c9 100644
--- a/cppcache/src/Cache.cpp
+++ b/cppcache/src/Cache.cpp
@@ -112,16 +112,19 @@
           new CacheImpl(this, dsProp, ignorePdxUnreadFields, readPdxSerialized,
                         authInitialize))) {}
 
-Cache::Cache(Cache&& other) noexcept
-    : m_cacheImpl(std::move(other.m_cacheImpl)) {
-  m_cacheImpl->setCache(this);
+Cache::Cache(Cache&& other) noexcept {
+  other.m_cacheImpl->doIfDestroyNotPending([&]() {
+    m_cacheImpl = std::move(other.m_cacheImpl);
+    m_cacheImpl->setCache(this);
+  });
 }
 
 Cache& Cache::operator=(Cache&& other) noexcept {
-  if (this != &other) {
+  other.m_cacheImpl->doIfDestroyNotPending([&]() {
     m_cacheImpl = std::move(other.m_cacheImpl);
     m_cacheImpl->setCache(this);
-  }
+  });
+
   return *this;
 }