1313#include < vector>
1414#include < unordered_map>
1515#include < iostream>
16+ #include < mutex>
1617
1718typedef std::string GCString;
1819
@@ -126,6 +127,7 @@ class GC final
126127 private:
127128 GCMap<GCString, ServerInfo> mServers ;
128129 Regions mServerRegion ;
130+ mutable std::mutex mMutex ;
129131
130132}; // class GC
131133
@@ -167,6 +169,8 @@ bool GC::AddServer(
167169 const ServerInfo& server
168170)
169171{
172+ std::scoped_lock lock (mMutex );
173+
170174 if (server.region == Regions::None)
171175 {
172176 LOG (" Cannot add server with region 'None'\n " );
@@ -183,11 +187,15 @@ bool GC::RemoveServer(
183187 const GCString& name
184188)
185189{
190+ std::scoped_lock lock (mMutex );
191+
186192 auto it = mServers .find (name);
187193 if (it == mServers .end ())
188194 return false ;
195+
189196 mServers .erase (it);
190- std::cout << " [LOG] Removed server: " << name << " \n " ;
197+
198+ LOG (" Removed server: " + name);
191199 return true ;
192200} // bool GC::RemoveServer
193201
@@ -203,14 +211,19 @@ ServerInfo* GC::FindServer(
203211
204212GCVector<ServerInfo> GC::ListServers () const
205213{
214+ std::scoped_lock lock (mMutex );
215+
206216 GCVector<ServerInfo> list;
207217 for (const auto & kv : mServers )
208218 list.push_back (kv.second );
219+
209220 return list;
210221} // GCVector<ServerInfo> GC::ListServers
211222
212223bool GC::SetServerStatus (const GCString& name, bool isOnline)
213224{
225+ std::scoped_lock lock (mMutex );
226+
214227 auto it = mServers .find (name);
215228 if (it == mServers .end ())
216229 return false ;
@@ -223,6 +236,8 @@ bool GC::SetServerStatus(const GCString& name, bool isOnline)
223236
224237GCVector<ServerInfo> GC::ListServers (Regions filterRegion, bool onlyOnline) const
225238{
239+ std::scoped_lock lock (mMutex );
240+
226241 GCVector<ServerInfo> list;
227242 for (const auto & kv : mServers )
228243 {
0 commit comments