BlockingList: fix unsafe getLockGuard() API

getLockGuard relies on copy elision to work correctly, which the compiler
is not required to do (only with C++17).
If no copy elision happens, the mutex ends up being unlocked twice, and the
CS is executed with the mutex unlocked.

The patch also ensures that the same pattern cannot be used again.
This commit is contained in:
Beat Küng
2019-11-05 12:14:20 +01:00
committed by Julian Oes
parent 4ff4f5c77f
commit a203475489
3 changed files with 7 additions and 4 deletions
+1 -1
View File
@@ -80,7 +80,7 @@ public:
List<T>::clear();
}
LockGuard getLockGuard() { return LockGuard{_mutex}; }
pthread_mutex_t &mutex() { return _mutex; }
private:
+3
View File
@@ -44,6 +44,9 @@ public:
pthread_mutex_lock(&_mutex);
}
LockGuard(const LockGuard &other) = delete;
LockGuard &operator=(const LockGuard &other) = delete;
~LockGuard()
{
pthread_mutex_unlock(&_mutex);