避免死锁

This commit is contained in:
xiongziliang
2020-04-06 21:23:35 +08:00
parent e192931429
commit 45c5f1ec4c
3 changed files with 28 additions and 8 deletions

View File

@@ -119,8 +119,15 @@ bool MediaSource::isRecording(Recorder::type type){
}
void MediaSource::for_each_media(const function<void(const MediaSource::Ptr &src)> &cb) {
lock_guard<recursive_mutex> lock(g_mtxMediaSrc);
for (auto &pr0 : g_mapMediaSrc) {
decltype(g_mapMediaSrc) copy;
{
//拷贝g_mapMediaSrc后再遍历考虑到是高频使用的全局单例锁并且在上锁时会执行回调代码
//很容易导致多个锁交叉死锁的情况,而且该函数使用频率不高,拷贝开销相对来说是可以接受的
lock_guard<recursive_mutex> lock(g_mtxMediaSrc);
copy = g_mapMediaSrc;
}
for (auto &pr0 : copy) {
for (auto &pr1 : pr0.second) {
for (auto &pr2 : pr1.second) {
for (auto &pr3 : pr2.second) {