public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/108299] New: toplevel thread_local variables are not initialized if not referenced and initialized at wrong moment when referenced
@ 2023-01-05 10:51 agriff at tin dot it
  2023-01-05 11:41 ` [Bug c++/108299] " jakub at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: agriff at tin dot it @ 2023-01-05 10:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108299

            Bug ID: 108299
           Summary: toplevel thread_local variables are not initialized if
                    not referenced and initialized at wrong moment when
                    referenced
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: agriff at tin dot it
  Target Milestone: ---

The standard mandates that thread_local variables at top level must be
initialized in case anything in the compilation unit is referred to. There is a
specific note on the fact that they must be initialized even if they're not
used in the program.
Also they must be initialized at thread start or before anything from the
compilation unit is accessed by the thread.

This code however shows that g++ doesn't follow this rule

    #include <stdio.h>

    thread_local int flag = ([](){
        printf("HERE!\n");
        return 1;
    })();

    int main() {
        printf("Hello, world.\n");
        return 0;
    }

In this version the thread local initialization is skipped.
Mover if after the printf the statement

    flag;

is added then the initialization is performed, but AFTER the "Hello, world"
message (another violation of the standard).

Trying on godbolt I saw the problem is present even in trunk and in clang
(works as expected in MSVC).

The problems are present both on the main implicit thread and with threads
started explicitly with std::thread.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-01-06 20:58 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-05 10:51 [Bug c++/108299] New: toplevel thread_local variables are not initialized if not referenced and initialized at wrong moment when referenced agriff at tin dot it
2023-01-05 11:41 ` [Bug c++/108299] " jakub at gcc dot gnu.org
2023-01-05 11:41 ` jakub at gcc dot gnu.org
2023-01-05 14:38 ` agriff at tin dot it
2023-01-05 14:47 ` jakub at gcc dot gnu.org
2023-01-05 14:52 ` agriff at tin dot it
2023-01-05 15:17 ` jakub at gcc dot gnu.org
2023-01-05 15:40 ` redi at gcc dot gnu.org
2023-01-05 15:42 ` redi at gcc dot gnu.org
2023-01-05 15:53 ` agriff at tin dot it
2023-01-06 19:57 ` jason at gcc dot gnu.org
2023-01-06 20:58 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).