Skip to content

Conversation

@ferdymercury
Copy link
Collaborator

This Pull request:

Changes or fixes:

Fixes #18751

This applies the suggestion of Dr15Jones

Checklist:

  • tested changes locally
  • updated the docs (if necessary)

@ferdymercury ferdymercury added this to the 6.38.00 milestone May 16, 2025
Copy link
Contributor

@guitargeek guitargeek left a comment

Choose a reason for hiding this comment

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

It's possible to determine the lengths at compile time. Like this, we don't need to rely on static variables here at all.

@guitargeek
Copy link
Contributor

guitargeek commented May 16, 2025

Maybe I'm missing something, but how does adding const make this thread safe? if two threads call the function at the same time for the first time, would initLengthsVector() not be called concurrently still? Anyway with constexpr there won't be a problem.

edit: actually, static const is safe: https://stackoverflow.com/questions/8102125/is-local-static-variable-initialization-thread-safe-in-c11 Well, so I guess the constexpr version is just an optimization :)

@pcanal
Copy link
Member

pcanal commented May 16, 2025

Maybe I'm missing something, but how does adding const make this thread safe? if two threads call the function at the same time for the first time, would initLengthsVector() not be called concurrently still

Just the static make the initialization thread safe. The C++ standard guarantees that a function static variable initialization is thread safe (and this is the reason for initialization pattern using a lambda function). The const only adds a guarantee that there will be no inadvertent updates to the static values (those would of course not be inherently thread-safe and would need their own locking mechanism)

Copy link
Member

@pcanal pcanal left a comment

Choose a reason for hiding this comment

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

In practice this is a NFC change since the variable were never modified but a good protection against inadvertent changes. Thanks.

@pcanal pcanal requested a review from guitargeek May 16, 2025 17:55
@ferdymercury
Copy link
Collaborator Author

In practice this is a NFC change since the variable were never modified but a good protection against inadvertent changes. Thanks.

Yep, i tried to reflect that in the PR title

@github-actions
Copy link

Test Results

    19 files      19 suites   3d 16h 56m 45s ⏱️
 2 746 tests  2 746 ✅ 0 💤 0 ❌
50 731 runs  50 731 ✅ 0 💤 0 ❌

Results for commit 1f02576.

@pcanal
Copy link
Member

pcanal commented May 16, 2025

See #18757

@pcanal pcanal closed this May 16, 2025
@ferdymercury ferdymercury deleted the patch-2 branch May 16, 2025 23:00
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.

Better thread safety in TClassEdit::CleanType

3 participants