diff --git a/frameworks/native/include/sensor_agent_proxy.h b/frameworks/native/include/sensor_agent_proxy.h index 5d58e1126257e9a5c137c0f866cee8838b2f5620..e1e44219eee990a7ff0b6427dfed8b29bc7bb479 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 f5d7368cd421ff6a4a617e149d8d64f9eba7bb20..61cdfb01bfe68415609d0b314c10a7a9bb38f372 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()) @@ -256,6 +257,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"); @@ -304,6 +306,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) {