diff --git a/include/Client.h b/include/Client.h index 260071c..8635387 100644 --- a/include/Client.h +++ b/include/Client.h @@ -36,6 +36,7 @@ public: void SetDownSock(SOCKET CSock) { mSocket[1] = CSock; } void SetTCPSock(SOCKET CSock) { mSocket[0] = CSock; } void SetStatus(int Status) { mStatus = Status; } + // locks void DeleteCar(int Ident); [[nodiscard]] std::set GetIdentifiers() const { return mIdentifiers; } [[nodiscard]] sockaddr_in GetUDPAddr() const { return mUDPAddress; } diff --git a/src/Client.cpp b/src/Client.cpp index e50e99c..7a0371f 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -47,24 +47,28 @@ TClient::TVehicleDataLockPair TClient::GetAllCars() { } std::string TClient::GetCarData(int Ident) { - std::unique_lock lock(mVehicleDataMutex); - for (auto& v : mVehicleData) { - if (v.ID() == Ident) { - return v.Data(); + { // lock + std::unique_lock lock(mVehicleDataMutex); + for (auto& v : mVehicleData) { + if (v.ID() == Ident) { + return v.Data(); + } } - } + } // unlock DeleteCar(Ident); return ""; } void TClient::SetCarData(int Ident, const std::string& Data) { - std::unique_lock lock(mVehicleDataMutex); - for (auto& v : mVehicleData) { - if (v.ID() == Ident) { - v.SetData(Data); - return; + { // lock + std::unique_lock lock(mVehicleDataMutex); + for (auto& v : mVehicleData) { + if (v.ID() == Ident) { + v.SetData(Data); + return; + } } - } + } // unlock DeleteCar(Ident); }