diff --git a/frameworks/native/include/sensor_agent_proxy.h b/frameworks/native/include/sensor_agent_proxy.h index 5bac0b3bba86e7825724844cefeb12806903f3f4..ed1a89d598496972f6ffe6401f785f042e919e66 100644 --- a/frameworks/native/include/sensor_agent_proxy.h +++ b/frameworks/native/include/sensor_agent_proxy.h @@ -67,6 +67,7 @@ private: int64_t reportInterval_ = -1; std::map> subscribeMap_; std::map> unsubscribeMap_; + static std::mutex createChannelMutex_; }; #define SENSOR_AGENT_IMPL OHOS::DelayedSingleton::GetInstance() diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index cf7a43f1eb8eeda88638e0d623f2f8487b563eaa..3fa993b2e2426d50fe6f6b89864cd632bde974e2 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -38,6 +38,7 @@ int32_t sensorInfoCount_ = 0; #define SEN_CLIENT SensorServiceClient::GetInstance() std::recursive_mutex SensorAgentProxy::subscribeMutex_; std::mutex SensorAgentProxy::chanelMutex_; +std::mutex SensorAgentProxy::createChannelMutex_; SensorAgentProxy::SensorAgentProxy() : dataChannel_(new (std::nothrow) SensorDataChannel()) @@ -250,6 +251,7 @@ int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *us SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return PARAMETER_ERROR; } + std::lock_guard createChannelLock(createChannelMutex_); int32_t ret = CreateSensorDataChannel(); if (ret != ERR_OK) { SEN_HILOGE("Create sensor data chanel failed"); @@ -297,6 +299,7 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * unsubscribeMap_.erase(sensorId); } } + std::lock_guard createChannelLock(createChannelMutex_); if (IsSubscribeMapEmpty()) { int32_t ret = DestroySensorDataChannel(); if (ret != ERR_OK) {