Did my compiler ignore my unused static thread_local class member? - c++

I want do some thread registration in my class, so I decide to add a check for the the thread_local feature:
#include <iostream>
#include <thread>
class Foo {
public:
Foo() {
std::cout << "Foo()" << std::endl;
}
~Foo() {
std::cout << "~Foo()" << std::endl;
}
};
class Bar {
public:
Bar() {
std::cout << "Bar()" << std::endl;
//foo;
}
~Bar() {
std::cout << "~Bar()" << std::endl;
}
private:
static thread_local Foo foo;
};
thread_local Foo Bar::foo;
void worker() {
{
std::cout << "enter block" << std::endl;
Bar bar1;
Bar bar2;
std::cout << "exit block" << std::endl;
}
}
int main() {
std::thread t1(worker);
std::thread t2(worker);
t1.join();
t2.join();
std::cout << "thread died" << std::endl;
}
The code is simple. My Bar class has a static thread_local member foo. If a static thread_local Foo foo is created, it means a thread is created.
But when I run the code, nothing in the Foo() prints, and if I remove the comment in Bar's constructor, which uses foo, the code works fine.
I tried this on GCC (7.4.0) and Clang (6.0.0) and the results are the same.
I guess that the compiler discovered that foo is unused and do not generate an instance. So
Did the compiler ignore the static thread_local member? How can I
debug for this?
If so, why does a normal static member not have this problem?

There is no problem with your observation. [basic.stc.static]/2 prohibits eliminating variables with static storage duration:
If a variable with static storage duration has initialization or a
destructor with side effects, it shall not be eliminated even if it
appears to be unused, except that a class object or its copy/move may
be eliminated as specified in [class.copy].
This restriction is not present for other storage durations. In fact, [basic.stc.thread]/2 says:
A variable with thread storage duration shall be initialized before
its first odr-use and, if constructed, shall be destroyed on
thread exit.
This suggests that a variable with thread storage duration need not be constructed unless odr-used.
But why is this discrepancy?
For static storage duration, there is only one instance of a variable per program. The side effects of construction thereof can be significant (kinda like a program-wide constructor), so the side effects are required.
For thread local storage duration, however, there is a problem: an algorithm may start a lot of threads. For most of these threads, the variable is completely irrelevant. It would be hilarious if an external physics simulation library that calls std::reduce(std::execution::par_unseq, first, last) ends up creating a lot of foo instances, right?
Of course, there can be a legitimate use for side effects of the construction of variables of thread local storage duration that are not odr-used (e.g., a thread tracker). However, the advantage for guaranteeing this is not enough to compensate for the aforementioned drawback, so these variables are allowed to be eliminated as long as they aren't odr-used. (Your compiler can choose not to do, though. And you can also make your own wrapper around std::thread that takes care of this.)

I found this information in "ELF Handling For Thread-Local Storage" which can prove #L.F. 's answer
In addition the run-time support should avoid creating the
thread-local storage if it is not necessary. For instance, a loaded
module might only be used by one thread of the many which make up the
process. It would be a waste of memory and time to allocate the
storage for all threads. A lazy method is wanted. This is not much
extra burden since the requirement to handle dynamically loaded
objects already requires recognizing storage which is not yet
allocated. This is the only alternative to stopping all threads and
allocating storage for all threads before letting them run again.

Related

why static variable is allocated when program starts but initialize later?

I'm new here, I see Static variables in a function are initialized before the function is called for the first time., but I still don't know why it doesn't call the constructor before the function starts?
class Base
{
public:
Base();
~Base();
private:
};
Base::Base()
{
cout << "I'm Base" << endl;
}
Base::~Base()
{
}
int main()
{
cout << "start program!" << endl;
static Base b;
return 0;
}
When are static function variables allocated?, I think the case is almost the same with me..
Any help will be appreciated ^_^
Not quite. Static variables are initialised the first time they are encountered, which of course is not necessarily at the start of a function.
Objects with static storage duration have two phases of initialisation: Static phase and dynamic phase. Some static variables don't have dynamic initialisation at all. Those objects that do have dynamic initialisation are initially statically zero-initialised.
The static phase of initialisation happens when the program starts, before anything else. Thus, memory must also have been allocated before anything else.
The dynamic phase of initialisation cannot be instantaneous. Dynamic initialisation may have dependencies on initialisation of other static objects. Some objects are necessarily initialised before other objects. This is why dynamic initialisation happens after allocation.
For namespace scope variables with static storage, their dynamic initialisation happens either before main, or it may be deferred later in which case it happens before anything from that same translation unit is accessed or called (in practice, deferral happens when dynamic loading is involved).
For static local variables...
Static variables in a function are initialized before the function is called for the first time
Not exactly. Their dynamic initialisation always happens exactly when execution reaches them for the first time. That is always after the function is called; not before. For example:
void foo(bool bar)
{
if (bar) {
static T var;
}
}
var will not be initialised even when the function is called, if the provided argument is false.
The order of dynamic initialisation across translation units is unspecified. This would otherwise make it impossible to safely rely on initialisation of objects with static storage from other translation units, but the "initialisation on first use" behaviour of static local variables is a feature that allows exact control over the order of their initialisation, making it possible to rely on their initialisation even across translation unit boundaries.
I think, based on your comments, that the detail you are missing is that there are both global and local static variables.
Global static variables are initialized when the program is first loaded. Edit: Apparently this is not required behavior (though it is the most common) - initialization is allowed to be delayed. See comment by #walnut.
Example:
int main()
{
std::cout << "start program!" << std::endl;
return 0;
}
static Base b;
Output:
I'm Base
start program!
While static variables in functions are initialized the first time control passes over them. Example:
int main()
{
std::cout << "start program!" << std::endl;
static Base b;
return 0;
}
Output:
start program!
I'm Base
I have added a second example, as per #EvilTeach's comment, to show how the static is only initialized a single time despite multiple functioncalls. Also, added guard (from #eerorika's answer) to show how initialization only happens when execution actually reaches the variable.
void testFunc(bool test)
{
std::cout << "testFunc called with: " << test << std::endl;
if (test)
static Base b;
}
int main()
{
testFunc(false);
testFunc(true);
testFunc(true);
return 0;
}
Output:
testFunc called with: 0
testFunc called with: 1
I'm Base
testFunc called with: 1

Is it safe to wait for asynchronous work by joining in the destructor?

Suppose I have a class that may run some code asynchronously, and that asynchronous code uses that class instance to do things like call member functions, read data members, etc. Obviously the class instance must outlive the background thread in order for those accesses to be safe. It is sufficient to ensure this by joining the background thread in the destructor? For example:
#include <iostream>
#include <thread>
class foo final
{
public:
foo() = default;
void bar() {
std::cout << "Hopefully there's nothing wrong with using " << this << "\n";
}
void bar_async() {
if (!m_thread.joinable()) {
m_thread = std::thread{&foo::bar, this};
}
}
~foo() {
if (m_thread.joinable()) {
std::cout << "Waiting for " << m_thread.get_id() << "\n";
m_thread.join();
}
}
private:
std::thread m_thread;
};
int main() {
foo f;
f.bar_async();
}
Specifically, I'm worried about object lifetime rules:
For any object of class types whose destructor is not trivial, lifetime ends when the execution of the destructor begins.
... after the lifetime of an object has ended and before the storage which the object occupied is reused or released, the following uses of the glvalue expression that identifies that object are undefined: ...
Access to a non-static data member or a call to a non-static member function.
But to me, a strict reading of the above would also imply that calling this->bar() from inside ~foo() directly is undefined, which is "obviously" not the case.
cppreference is right but it is talking about accessing the members from the object, not from inside the destructor. If we look at [class.cdtor]/1 we see that
For an object with a non-trivial constructor, referring to any non-static member or base class of the object before the constructor begins execution results in undefined behavior. For an object with a non-trivial destructor, referring to any non-static member or base class of the object after the destructor finishes execution results in undefined behavior.
emphasis mine
So, as long as we are in the destructor we can still work with the member objects as those are not destroyed until the scope of the destructor ends.
So, calling join on the thread is fine here. If you think about it if it wasn't then things like lock guards would be useless if accessing the mutex they refer to was undefined behavior.
My intuition is no. This is because thread::join can throw an exception and you don't want exceptions escaping your destructor. It may be okay if you wrap it in a try catch and handle the exception properly though.

Is it legal to initialize a thread_local variable in the destructor of a global variable?

This program:
#include <iostream>
struct Foo {
Foo() {
std::cout << "Foo()\n";
}
~Foo() {
std::cout << "~Foo()\n";
}
};
struct Bar {
Bar() {
std::cout << "Bar()\n";
}
~Bar() {
std::cout << "~Bar()\n";
thread_local Foo foo;
}
};
Bar bar;
int main() {
return 0;
}
Prints
Bar()
~Bar()
Foo()
for me (GCC 6.1, Linux, x86-64). ~Foo() is never called. Is that the expected behaviour?
The Standard does not cover this case; the strictest reading would be that it is legal to initialize a thread_local in the destructor of an object with static storage duration, but it is illegal to allow the program to continue to normal completion.
The problem arises in [basic.start.term]:
1 - Destructors ([class.dtor]) for initialized objects (that is, objects whose lifetime ([basic.life]) has begun) with static storage duration are called as a result of returning from main and as a result of calling std::exit ([support.start.term]). Destructors for initialized objects with thread storage duration within a given thread are called as a result of returning from the initial function of that thread and as a result of that thread calling std::exit. The completions of the destructors for all initialized objects with thread storage duration within that thread are sequenced before the initiation of the destructors of any object with static storage duration. [...]
So the completion of bar::~Bar::foo::~Foo is sequenced before the initiation of bar::~Bar, which is a contradiction.
The only get-out could be to argue that [basic.start.term]/1 only applies to objects whose lifetime has begun at the point of program/thread termination, but contra [stmt.dcl] has:
5 - The destructor for a block-scope object with static or thread storage duration will be executed if and only if it was constructed. [ Note: [basic.start.term] describes the order in which block-scope objects with static and thread storage duration are destroyed. — end note ]
This is clearly intended to apply only to normal thread and program termination, by return from main or from a thread function, or by calling std::exit.
Also, [basic.stc.thread] has:
A variable with thread storage duration shall be initialized before its first odr-use ([basic.def.odr]) and, if constructed, shall be destroyed on thread exit.
The "shall" here is an instruction to the implementor, not to the user.
Note that there is nothing wrong with beginning the lifetime of the destructor-scoped thread_local, since [basic.start.term]/2 does not apply (it is not previously destroyed). That is why I believe that undefined behavior occurs when you allow the program to continue to normal completion.
Similar questions have been asked before, though about static vs. static storage duration rather than thread_local vs. static; Destruction of objects with static storage duration (and https://groups.google.com/forum/#!topic/comp.std.c++/Tunyu2IJ6w0), and Destructor of a static object constructed within the destructor of another static object. I'm inclined to agree with James Kanze on the latter question that [defns.undefined] applies here, and the behavior is undefined because the Standard does not define it. The best way forward would be for someone with standing to open a defect report (covering all the combinations of statics and thread_locals initialized within the destructors of statics and thread_locals), to hope for a definitive answer.
Write your program as
#include <iostream>
thread_local struct Foo {
Foo() { std::cout << "Foo()\n"; }
~Foo() { std::cout << "~Foo()\n"; }
} t;
struct Bar {
Bar() { std::cout << "Bar()\n"; }
~Bar() { std::cout << "~Bar()\n"; t; }
} b;
int main() {
return 0;
}
If Foo wasn't thread_local, then Foo t and Bar b are on equal position, and it's possible to destruct Foo t before Bar b.
In this case, when referring to t in b.~Bar(), it's referring to a destructed struct, which IMO should be a UB(on some system destructing struct free its memory).
Therefore, adding thread_local it's still undefined behavior

Is it ok to make std::once_flag a member (non static member)

I need to make some calculations and cache results for current object lazily (upon first request). I can do it with mutex obviously, but I am just curios is it ok to make std::once_flag a member and use it to initialize this instance. I used it for singletons but not for ordinary objects.
It's certainly possible to do what you suggest. Provided the once_flag is properly constructed before attempting to use it(a), it should work fine, and that can be guaranteed simply by having it as a member variable within the class, something like:
#include <iostream>
#include <mutex>
class MyClass {
public:
MyClass() {
std::cout << "Constructing\n";
}
void DoSomething() {
std::call_once(m_lazyInit, []() {
std::cout << "Lazy init\n";
});
std::cout << "Doing something\n";
}
private:
std::once_flag m_lazyInit;
};
int main() {
MyClass x;
x.DoSomething(); // Sequential but would also work concurrently.
x.DoSomething();
return 0;
}
At any point after that, only one successful call to the lazy initialisation code will be made for your object regardless of how many times you call DoSomething():
Constructing
Lazy init
Doing something
Doing something
It seems that, from your comments, you're concerned it will be resource heavy and it may be less strenuous to use something like a static (class-level) mutex with a member boolean to do the same thing. That way, the possible-heavy-load threading stuff is limited to one item rather than one item per object (replacing just the class in the above code):
class MyClass {
public:
MyClass() : m_lazyInitFlag(true) {
std::cout << "Constructing\n";
}
void DoSomething() {
{ // Limit lock duration to as short as possible.
std::lock_guard<std::mutex> lazyInit(m_lazyInitMutex);
if (m_lazyInitFlag) {
std::cout << "Lazy init\n";
m_lazyInitFlag = false;
}
}
std::cout << "Doing something\n";
}
private:
static std::mutex m_lazyInitMutex;
bool m_lazyInitFlag;
};
std::mutex MyClass::m_lazyInitMutex;
However, I'd be less concerned about that. The people putting together your standard library will have optimised it to the maximum extent possible and will have almost certainly considered the possibility that you need per-object call-once() callables.
In addition, a solution like that static-mutex-member-flag option introduces more contention since you can only run one concurrent callable across the entire class rather then one in each object.
It's also going to mean that, should you wish to use callables that can fail, you'll need to handle that yourself with try..catch blocks rather than get the automatic handling provided by call_once().
My advice would be to just use the call_once() functionality since that really is its intended use case. Only worry about possible performance problems when they appear and, at that point, you can investigate other solutions.
In other words, YAGNI :-)
(a) As Barry points out in a comment, you'll also have a restriction on copying or moving your object since the non-copyable, non-movable once_flag member will prevent this.
So, if you need to copy or move your objects, you may want to use another option.

When do function-level static variables get allocated/initialized?

I'm quite confident that globally declared variables get allocated (and initialized, if applicable) at program start time.
int globalgarbage;
unsigned int anumber = 42;
But what about static ones defined within a function?
void doSomething()
{
static bool globalish = true;
// ...
}
When is the space for globalish allocated? I'm guessing when the program starts. But does it get initialized then too? Or is it initialized when doSomething() is first called?
I was curious about this so I wrote the following test program and compiled it with g++ version 4.1.2.
include <iostream>
#include <string>
using namespace std;
class test
{
public:
test(const char *name)
: _name(name)
{
cout << _name << " created" << endl;
}
~test()
{
cout << _name << " destroyed" << endl;
}
string _name;
};
test t("global variable");
void f()
{
static test t("static variable");
test t2("Local variable");
cout << "Function executed" << endl;
}
int main()
{
test t("local to main");
cout << "Program start" << endl;
f();
cout << "Program end" << endl;
return 0;
}
The results were not what I expected. The constructor for the static object was not called until the first time the function was called. Here is the output:
global variable created
local to main created
Program start
static variable created
Local variable created
Function executed
Local variable destroyed
Program end
local to main destroyed
static variable destroyed
global variable destroyed
Some relevant verbiage from C++ Standard:
3.6.2 Initialization of non-local objects [basic.start.init]
1
The storage for objects with static storage
duration (basic.stc.static) shall be zero-initialized (dcl.init)
before any other initialization takes place. Objects of
POD types (basic.types) with static storage duration
initialized with constant expressions (expr.const) shall be
initialized before any dynamic initialization takes place.
Objects of namespace scope with static storage duration defined in
the same translation unit and dynamically initialized shall be
initialized in the order in which their definition appears in
the translation unit. [Note: dcl.init.aggr describes the
order in which aggregate members are initialized. The
initialization of local static objects is described in stmt.dcl. ]
[more text below adding more liberties for compiler writers]
6.7 Declaration statement [stmt.dcl]
...
4
The zero-initialization (dcl.init) of all local objects with
static storage duration (basic.stc.static) is performed before
any other initialization takes place. A local object of
POD type (basic.types) with static storage duration
initialized with constant-expressions is initialized before its
block is first entered. An implementation is permitted to perform
early initialization of other local objects with static storage
duration under the same conditions that an implementation is
permitted to statically initialize an object with static storage
duration in namespace scope (basic.start.init). Otherwise such
an object is initialized the first time control passes through its
declaration; such an object is considered initialized upon the
completion of its initialization. If the initialization exits by
throwing an exception, the initialization is not complete, so it will
be tried again the next time control enters the declaration. If control re-enters the declaration (recursively) while the object is being
initialized, the behavior is undefined. [Example:
int foo(int i)
{
static int s = foo(2*i); // recursive call - undefined
return i+1;
}
--end example]
5
The destructor for a local object with static storage duration will
be executed if and only if the variable was constructed.
[Note: basic.start.term describes the order in which local
objects with static storage duration are destroyed. ]
The memory for all static variables is allocated at program load. But local static variables are created and initialized the first time they are used, not at program start up. There's some good reading about that, and statics in general, here. In general I think some of these issues depend on the implementation, especially if you want to know where in memory this stuff will be located.
The compiler will allocate static variable(s) defined in a function foo at program load, however the compiler will also add some additional instructions (machine code) to your function foo so that the first time it is invoked this additional code will initialize the static variable (e.g. invoking the constructor, if applicable).
#Adam: This behind the scenes injection of code by the compiler is the reason for the result you saw.
I try to test again code from Adam Pierce and added two more cases: static variable in class and POD type. My compiler is g++ 4.8.1, in Windows OS(MinGW-32).
Result is static variable in class is treated same with global variable. Its constructor will be called before enter main function.
Conclusion (for g++, Windows environment):
Global variable and static member in class: constructor is called before enter main function (1).
Local static variable: constructor is only called when execution reaches its declaration at first time.
If Local static variable is POD type, then it is also initialized before enter main function (1).
Example for POD type: static int number = 10;
(1): The correct state should be: "before any function from the same translation unit is called". However, for simple, as in example below, then it is main function.
#include <iostream>
#include <string>
using namespace std;
class test
{
public:
test(const char *name)
: _name(name)
{
cout << _name << " created" << endl;
}
~test()
{
cout << _name << " destroyed" << endl;
}
string _name;
static test t; // static member
};
test test::t("static in class");
test t("global variable");
void f()
{
static test t("static variable");
static int num = 10 ; // POD type, init before enter main function
test t2("Local variable");
cout << "Function executed" << endl;
}
int main()
{
test t("local to main");
cout << "Program start" << endl;
f();
cout << "Program end" << endl;
return 0;
}
result:
static in class created
global variable created
local to main created
Program start
static variable created
Local variable created
Function executed
Local variable destroyed
Program end
local to main destroyed
static variable destroyed
global variable destroyed
static in class destroyed
Anybody tested in Linux env ?
Or is it initialized when doSomething() is first called?
Yes, it is. This, among other things, lets you initialize globally-accessed data structures when it is appropriate, for example inside try/catch blocks. E.g. instead of
int foo = init(); // bad if init() throws something
int main() {
try {
...
}
catch(...){
...
}
}
you can write
int& foo() {
static int myfoo = init();
return myfoo;
}
and use it inside the try/catch block. On the first call, the variable will be initialized. Then, on the first and next calls, its value will be returned (by reference).
Static variables are allocated inside a code segment -- they are part of the executable image, and so are mapped in already initialized.
Static variables within function scope are treated the same, the scoping is purely a language level construct.
For this reason you are guaranteed that a static variable will be initialized to 0 (unless you specify something else) rather than an undefined value.
There are some other facets to initialization you can take advantage off -- for example shared segments allow different instances of your executable running at once to access the same static variables.
In C++ (globally scoped) static objects have their constructors called as part of the program start up, under the control of the C runtime library. Under Visual C++ at least the order that objects are initialized in can be controlled by the init_seg pragma.
In the following code it prints Initial = 4 which is the value of static_x as it is implemented in the compiling time.
int func(int x)
{
static int static_x = 4;
static_x = x;
printf ("Address = 0x%x",&static_x ); // prints 0x40a010
return static_x;
}
int main()
{
int x = 8;
uint32_t *ptr = (uint32_t *)(0x40a010); // static_x location
printf ("Initial = %d\n",*ptr);
func(x);
return 0;
}