MISRA Discussion Forums
Rule 6.7.1 and thread storage duration - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=188)
+---- Forum: 4.6 Basic concepts (https://forum.misra.org.uk/forumdisplay.php?fid=192)
+---- Thread: Rule 6.7.1 and thread storage duration (/showthread.php?tid=1702)



Rule 6.7.1 and thread storage duration - rg99 - 10-09-2024

Is Rule 6.7.1 intended to apply to local variables with thread storage duration?  For example:

Code:
typedef int int32_t;

void foo() {
    static int32_t var1{0};        // Non-compliant
    thread_local int32_t var2{0};  // Compliant ???
    /* ... */
}


While the rule specifically references "static storage duration" it is not clear what the intended impact of this rule is with respect to "thread storage duration" variables.  While local variables with thread storage duration do not suffer the same intrinsically thread-unsafe characteristics as those with static storage duration, the provided Rationale still applies.  In particular, they decrease comprehensibility, introduce temporal coupling, and can result in (reentrant) data races.


RE: Rule 6.7.1 and thread storage duration - misra cpp - 13-09-2024

We should have said that 'thread_local' is equivalent to 'static' for this rule and indeed for 11.6.1 as well  (All variables should be initialized).

See C++::2017  [basic_start_static] para 2.

A note has been made to update the document accordingly