Minor smell cleanup - #8691
Conversation
nbolton
left a comment
There was a problem hiding this comment.
Purely academic, but: What is the rationale for using scoped_lock on single mutexes? As I understand it, performance-wise, lock_guard is the same for single mutexes but some consider scoped_lock to be less safe:
For the common case that one needs to lock exactly one mutex, std::lock_guard has an API that is a little safer to use than scoped_lock.
https://stackoverflow.com/a/60172828/47775
From Sonarscan: std::scoped_lock basically provides the same feature as std::lock_guard, but is more generic: It can lock several mutexes at the same time, with a deadlock prevention mechanism (see S5524). The equivalent code to perform simultaneous locking with std::lock_guard is significantly more complex. Therefore, it is simpler to use std::scoped_lock all the time, even when locking only one mutex (there will be no performance impact).std::scoped_lock basically provides the same feature as std::lock_guard, but is more generic: It can lock several mutexes at the same time, with a deadlock prevention mechanism (see S5524). The equivalent code to perform simultaneous locking with std::lock_guard is significantly more complex. Therefore, it is simpler to use std::scoped_lock all the time, even when locking only one mutex (there will be no performance impact). |
227fb7c to
7958d37
Compare
Yes, I agree with this. What do you think about what Howard Hinnant is saying? He is saying that |
std::scoped_lockin place ofstd::lock_guard