bool MediaAudioPlayer::play()
{
std::lock_guard<std::recursive_mutex> lock(m_mutex);
DLT_LOG_FCN_CXX(playerContext, DLT_LOG_DEBUG, "");
DLT_LOG_CXX(playerContext, DLT_LOG_DEBUG, "mSourceId =", m_sourceId, "mSourceType =", toString(m_sourceType), "mCurrentTrackPath =", m_currentTrackPath);
std::string filePath {};
if(m_pMediaTracklistManager != nullptr)
{
filePath += m_pMediaTracklistManager->GetCurrentTrack(m_sourceId, m_sourceType);
}
if(filePath.empty())
{
DLT_LOG_CXX(playerContext, DLT_LOG_DEBUG, "new filePath is empty, can't play");
return false;
}
DLT_LOG_CXX(playerContext, DLT_LOG_DEBUG, "filePath to play =", filePath, "mIsFileOpened =", m_isFileOpened);
bool res = true;
if (m_isFileOpened == false || (filePath.compare(m_currentTrackPath) != 0))
{
res = this->openStream(filePath);
if (m_currentTrackPosition != 0)
{
this->pause();
this->seek(m_currentTrackPosition);
return res;
}
}
else
{
DLT_LOG_CXX(playerContext, DLT_LOG_DEBUG, "File is already opened");
}
if (res == true)
{
res = this->playStream();
}
return res;
}
问题:C++中std::lock_guard<std::recursive_mutex> lock(m_mutex);这一句如何理解?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- 急速光粒 2021-09-17 11:52关注
std::lock_guardstd::recursive_mutex lock(m_mutex);
std::lock_guard:是std库提供的锁模板类。
std::recursive_mutex:是std库的互斥类,std::lock_guardstd::recursive_mutex就是互斥锁。
lock(m_mutex);是用m_mutex互斥量初始化了互斥锁。
目的就是给数据加了个互斥锁,用于保护临界区,确保同一时间只有一个线程访问数据。
对共享资源的访问,如互斥量已经上锁,调用线程会阻塞,直到互斥量被解锁。在完成了对共享资源的访问后,要对互斥量进行解锁。
有帮助请采纳,谢谢!本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 matlab不知道怎么改,求解答!!
- ¥15 永磁直线电机的电流环pi调不出来
- ¥15 用stata实现聚类的代码
- ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
- ¥20 docker里部署springboot项目,访问不到扬声器
- ¥15 netty整合springboot之后自动重连失效
- ¥15 悬赏!微信开发者工具报错,求帮改
- ¥20 wireshark抓不到vlan
- ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
- ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持