From d8cee7085b3951173dedfdd6a20b585446c1115c Mon Sep 17 00:00:00 2001 From: Daniel Cheng Date: Tue, 22 Aug 2023 13:44:36 -0700 Subject: [PATCH] Update C++ style guide. - Update caveats for `thread_local` usage, particularly around the risk of destruction order issues. - Provide explicit guidance for situations where `bit_cast` may be a better fit than `reinterpret_cast`. - Emphasize that kConstant-style naming can still be used for `const` automatic variables that are initialized at runtime, but only if the resulting variable has the same value with each evaluation (i.e. it does not depend on runtime inputs). - Clarify what sorts of details belong in file-level comments vs comments for individual abstractions. - Update TODO examples to reflect the preferred styling, from most preferred to least preferred. --- cppguide.html | 138 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 83 insertions(+), 55 deletions(-) diff --git a/cppguide.html b/cppguide.html index 788f63a..ffb061d 100644 --- a/cppguide.html +++ b/cppguide.html @@ -1044,9 +1044,12 @@ thread, rather than once at program startup. This means that other thread_local variables are subject to the same initialization-order issues as static variables (and more besides).

-

thread_local variable instances are not destroyed before their -thread terminates, so they do not have the destruction-order issues of static -variables.

+

thread_local variables have a subtle destruction-order issue: +during thread shutdown, thread_local variables will be destroyed +in the opposite order of their initialization (as is generally true in C++). +If code triggered by the destructor of any thread_local variable +refers to any already-destroyed thread_local on that thread, we will +get a particularly hard to diagnose use-after-free.