diff --git a/services/src/sensor_service.cpp b/services/src/sensor_service.cpp index e751179af97dd76582314f316b0ac11237b1fc5a..e9e91b246b2bffd70a93e827371011089aa73f50 100644 --- a/services/src/sensor_service.cpp +++ b/services/src/sensor_service.cpp @@ -577,12 +577,16 @@ ErrCode SensorService::GetSensorList(std::vector &sensorList) ErrCode SensorService::GetSensorListByDevice(int32_t deviceId, std::vector &singleDevSensors) { CALL_LOG_ENTER; - for (const auto& sensor : sensors_) { - if (sensor.GetDeviceId() == deviceId) { - SEN_HILOGD("Sensor found: GetDeviceId: %{public}d, deviceId: %{public}d", sensor.GetDeviceId(), deviceId); - singleDevSensors.push_back(sensor); + { + std::lock_guard sensorLock(sensorsMutex_); + for (const auto& sensor : sensors_) { + if (sensor.GetDeviceId() == deviceId) { + SEN_HILOGD("Sensor found: id is %{public}d", deviceId); + singleDevSensors.push_back(sensor); + } } } + if (singleDevSensors.empty()) { std::vector sensors = GetSensorListByDevice(deviceId); int32_t sensorCount = static_cast(sensors.size()); @@ -784,6 +788,7 @@ int32_t SensorService::Dump(int32_t fd, const std::vector &args) [](const std::u16string &arg) { return Str16ToStr8(arg); }); + std::lock_guard sensorLock(sensorsMutex_); sensorDump.ParseCommand(fd, argList, sensors_, clientInfo_); return ERR_OK; }