Skip to content

Minor smell cleanup - #8691

Merged
nbolton merged 3 commits into
masterfrom
minorSmellCleanup
Jun 17, 2025
Merged

Minor smell cleanup#8691
nbolton merged 3 commits into
masterfrom
minorSmellCleanup

Conversation

@sithlord48

Copy link
Copy Markdown
Member
  • Use std::scoped_lock in place of std::lock_guard
  • use Arch::time in place of ARCH->time()
  • use Arch::sleep() in place of ARCH->sleep()

@sithlord48
sithlord48 requested a review from nbolton June 16, 2025 00:32

@nbolton nbolton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@sithlord48

Copy link
Copy Markdown
Member Author

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:

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).

@sithlord48
sithlord48 requested a review from nbolton June 16, 2025 11:51
@nbolton

nbolton commented Jun 17, 2025

Copy link
Copy Markdown
Member

simpler to use std::scoped_lock all the time

Yes, I agree with this. What do you think about what Howard Hinnant is saying? He is saying that scoped_lock is less safe because you can accidentally lock 0 mutexes. TBH, I don't want to get bogged down in academic discussion, just curious about your perspective. From the reviewer's perspective: It's more work to review because I have to check that the mutex was passed to the lock in each code change, and there is a possibility that we missed one.

@nbolton nbolton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and working well.

@nbolton
nbolton merged commit c15214a into master Jun 17, 2025
@nbolton
nbolton deleted the minorSmellCleanup branch June 17, 2025 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants