When is a `thread_local` global variable initialized?

前端 未结 1 1554
醉话见心
醉话见心 2020-12-17 09:34

Consider the following example (lock guards on cout omitted for simplicity).

#include 
#include 
#include 

        
相关标签:
1条回答
  • 2020-12-17 09:51

    The standard allows for this behavior, although it doesn't guarantee it. From 3.7.2/2 [basic.stc.thread]:

    A variable with thread storage duration shall be initialized before its first odr-use (3.2) and, if constructed, shall be destroyed on thread exit.

    It's also possible that the objects are constructed at some other time (e.g. on program startup), as "before first use" means "at any point as long as it is before" rather than does "just before".

    0 讨论(0)
提交回复
热议问题