deallocation order of the static member and detached thread itself - c++

There is a global function that initializes and cleans up some resources.
But those 2 function must be called when no other thread is running.(like curl_global_init)
Initializer must be manually called, but I don't want the cleaner to be called manually.
So I do the following.
class GLOBAL_WRAPPER {
public:
static void init() {
getInstance();
}
static GLOBAL_WRAPPER& getInstance() {
static GLOBAL_WRAPPER inst;
return inst;
}
private:
GLOBAL_WRAPPER() {
// do global init call
}
~GLOBAL_WRAPPER() {
// do global cleanup call
}
};
void GLOBAL_INIT() {
GLOBAL_WRAPPER::init();
}
int main(){
GLOBAL_INIT();
// do whatever you want
// std::thread([](){for(;;);}).detach(); oops!
}
But in such bad case like: creating detached thread and not terminating it before main ends, when is deallocation of static variables(GLOBAL_WRAPPER in this case) called?
detached thread is terminated and static variable is freed
static variable is freed and detached thread is terminated
implementation defined.
I'm just interested in the thread itself, not thread storage duration objects.

The C++ standard specifies (in so many words) that returning from main is equivalent to calling std::exit.
main function [basic.start.main]
A return statement in main has the effect of leaving the main function
(destroying any objects with automatic storage duration) and calling
std::exit with the return value as the argument. If control flows off
the end of the compound-statement of main, the effect is equivalent to
a return with operand 0.
exit is defined as follows (irrelevant details omitted):
Startup and termination [support.start.term]
[[noreturn]] void exit(int status);
Effects:
— First, objects with thread storage duration and associated
with the current thread are destroyed. Next, objects with static
storage duration are destroyed and functions registered by calling
atexit are called.
— Next, all open C streams ... are removed.
— Finally, control is returned to the host environment. If status is
zero or EXIT_SUCCESS, an implementation-defined form of the status
successful termination is returned. If status is EXIT_- FAILURE, an
implementation-defined form of the status unsuccessful termination is
returned.
It is not defined what happens if non-main execution threads are running when the main execution thread returns. The shown code does not guarantee that the non-main execution thread terminates/sequenced before main returns.
As such, the only thing the standard specifies is that:
Global objects get destroyed
"Control is returned to the host environment", a.k.a.: "It's dead, Jim".
The standard does not define what happens if non-main execution threads are running when the main execution thread returns. A.k.a.: undefined behavior.

Related

static object intialization and compilation c++ [duplicate]

If a variable is declared as static in a function's scope it is only initialized once and retains its value between function calls. What exactly is its lifetime? When do its constructor and destructor get called?
void foo()
{
static string plonk = "When will I die?";
}
The lifetime of function static variables begins the first time[0] the program flow encounters the declaration and it ends at program termination. This means that the run-time must perform some book keeping in order to destruct it only if it was actually constructed.
Additionally, since the standard says that the destructors of static objects must run in the reverse order of the completion of their construction[1], and the order of construction may depend on the specific program run, the order of construction must be taken into account.
Example
struct emitter {
string str;
emitter(const string& s) : str(s) { cout << "Created " << str << endl; }
~emitter() { cout << "Destroyed " << str << endl; }
};
void foo(bool skip_first)
{
if (!skip_first)
static emitter a("in if");
static emitter b("in foo");
}
int main(int argc, char*[])
{
foo(argc != 2);
if (argc == 3)
foo(false);
}
Output:
C:>sample.exe
Created in foo
Destroyed in foo
C:>sample.exe 1
Created in if
Created in foo
Destroyed in foo
Destroyed in if
C:>sample.exe 1 2
Created in foo
Created in if
Destroyed in if
Destroyed in foo
[0] Since C++98[2] has no reference to multiple threads how this will be behave in a multi-threaded environment is unspecified, and can be problematic as Roddy mentions.
[1] C++98 section 3.6.3.1 [basic.start.term]
[2] In C++11 statics are initialized in a thread safe way, this is also known as Magic Statics.
Motti is right about the order, but there are some other things to consider:
Compilers typically use a hidden flag variable to indicate if the local statics have already been initialized, and this flag is checked on every entry to the function. Obviously this is a small performance hit, but what's more of a concern is that this flag is not guaranteed to be thread-safe.
If you have a local static as above, and foo is called from multiple threads, you may have race conditions causing plonk to be initialized incorrectly or even multiple times. Also, in this case plonk may get destructed by a different thread than the one which constructed it.
Despite what the standard says, I'd be very wary of the actual order of local static destruction, because it's possible that you may unwittingly rely on a static being still valid after it's been destructed, and this is really difficult to track down.
The existing explanations aren't really complete without the actual rule from the Standard, found in 6.7:
The zero-initialization of all block-scope variables with static storage duration or thread storage duration is performed before any other initialization takes place. Constant initialization of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered. An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope. Otherwise such a variable is initialized the first time control passes through its declaration; such a variable 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 enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined.
FWIW, Codegear C++Builder doesn't destruct in the expected order according to the standard.
C:\> sample.exe 1 2
Created in foo
Created in if
Destroyed in foo
Destroyed in if
... which is another reason not to rely on the destruction order!
The Static variables are come into play once the program execution starts and it remain available till the program execution ends.
The Static variables are created in the Data Segment of the Memory.

Singleton with an object which throws in the ctor - accessing again?

I can use C++11 or C++14 (or even C++17). Let's suppose I have a singleton object
class MyInstance {
public:
MyInstance() {
throw std::runtime_exception("something went wrong"); // Ctor might throw
}
};
MyInstance& getInstance() {
static MyInstance obj;
return obj;
}
Now, I made sure that every call to getInstance is wrapped in a
try {
auto& inst = getInstance();
} catch(std::runtime_error& e) {
// do something
}
What I'm wondering right now is: what happens if after failing an initialization in the constructor and throwing and catching and informing the user in the logs... the program passes again in a try codepath and calls getInstance again?
I made a few guesses but I have no idea if they're right:
The object has static storage so it will only be attempted to be built once I think?
Will returning a reference to an unconstructed object will get me a dangling reference and undefined behavior?
Would use a unique_ptr as static variable in place of obj solve this problem so I can access the pointer multiple times and also check if the object was properly constructed (if (uptr == TRUE)) ?
If the constructor throws the object is not initialized. So if control passes through getInstance again, initialization will be performed again as well.
[stmt.dcl] (emphasis mine)
4 Dynamic initialization of a block-scope variable with static
storage duration or thread storage duration is performed the first
time control passes through its declaration; such a variable 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 enters the declaration
concurrently while the variable is being initialized, the concurrent
execution shall wait for completion of the initialization. If
control re-enters the declaration recursively while the variable is
being initialized, the behavior is undefined.
[stmt.dcl]/4: Dynamic initialization of a block-scope variable with static storage duration or thread storage duration is performed the first time control passes through its declaration; such a variable 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. [..]
No need to "guess"; you could put a std::cout trace inside MyInstance::MyInstance() and call getInstance() twice. 😊
Also no need for smart pointers; the object either exists or it doesn't, and there's no way to proceed inside getInstance() after the declaration without the object existing, because you threw an exception!
By the way, it's std::runtime_error, not std::runtime_exception.

Lifetime of local static variables in C++11 [duplicate]

If a variable is declared as static in a function's scope it is only initialized once and retains its value between function calls. What exactly is its lifetime? When do its constructor and destructor get called?
void foo()
{
static string plonk = "When will I die?";
}
The lifetime of function static variables begins the first time[0] the program flow encounters the declaration and it ends at program termination. This means that the run-time must perform some book keeping in order to destruct it only if it was actually constructed.
Additionally, since the standard says that the destructors of static objects must run in the reverse order of the completion of their construction[1], and the order of construction may depend on the specific program run, the order of construction must be taken into account.
Example
struct emitter {
string str;
emitter(const string& s) : str(s) { cout << "Created " << str << endl; }
~emitter() { cout << "Destroyed " << str << endl; }
};
void foo(bool skip_first)
{
if (!skip_first)
static emitter a("in if");
static emitter b("in foo");
}
int main(int argc, char*[])
{
foo(argc != 2);
if (argc == 3)
foo(false);
}
Output:
C:>sample.exe
Created in foo
Destroyed in foo
C:>sample.exe 1
Created in if
Created in foo
Destroyed in foo
Destroyed in if
C:>sample.exe 1 2
Created in foo
Created in if
Destroyed in if
Destroyed in foo
[0] Since C++98[2] has no reference to multiple threads how this will be behave in a multi-threaded environment is unspecified, and can be problematic as Roddy mentions.
[1] C++98 section 3.6.3.1 [basic.start.term]
[2] In C++11 statics are initialized in a thread safe way, this is also known as Magic Statics.
Motti is right about the order, but there are some other things to consider:
Compilers typically use a hidden flag variable to indicate if the local statics have already been initialized, and this flag is checked on every entry to the function. Obviously this is a small performance hit, but what's more of a concern is that this flag is not guaranteed to be thread-safe.
If you have a local static as above, and foo is called from multiple threads, you may have race conditions causing plonk to be initialized incorrectly or even multiple times. Also, in this case plonk may get destructed by a different thread than the one which constructed it.
Despite what the standard says, I'd be very wary of the actual order of local static destruction, because it's possible that you may unwittingly rely on a static being still valid after it's been destructed, and this is really difficult to track down.
The existing explanations aren't really complete without the actual rule from the Standard, found in 6.7:
The zero-initialization of all block-scope variables with static storage duration or thread storage duration is performed before any other initialization takes place. Constant initialization of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered. An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope. Otherwise such a variable is initialized the first time control passes through its declaration; such a variable 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 enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined.
FWIW, Codegear C++Builder doesn't destruct in the expected order according to the standard.
C:\> sample.exe 1 2
Created in foo
Created in if
Destroyed in foo
Destroyed in if
... which is another reason not to rely on the destruction order!
The Static variables are come into play once the program execution starts and it remain available till the program execution ends.
The Static variables are created in the Data Segment of the Memory.

Is a future safe to pass to a detached thread?

Is passing a std::future to a detached instance of std::thread a safe operation? I know that underneath, the std::future has state in a shared_ptr which it shares with a std::promise. Here is an example.
int main()
{
std::promise<void> p;
std::thread( [f = p.get_future()]() {
if ( f.wait_for( std::chrono::seconds( 2 ) ) == std::future_status::ready )
{
return;
}
std::terminate();
} ).detach();
// wait for some operation
p.set_value();
}
There is a potential error case in the above code where the lambda is executed after the main thread exits. Does the shared state remain after the main thread exits?
[basic.start.term]/6 If there is a use of a standard library object or function not permitted within signal handlers (21.10) that does not happen before (4.7) completion of destruction of objects with static storage duration and execution of std::atexit registered functions (21.5), the program has undefined behavior.
Per [basic.start.main]/5, returning from main has the effect of calling std::exit, which does destroy objects with static storage duration and execute std::atexit registered functions. Therefore, I believe your example exhibits undefined behavior.
According to cppreference:
In a typical implementation, std::shared_ptr holds only two pointers:
- the stored pointer (one returned by get());
- a pointer to control block.
The control block is a dynamically-allocated object...
Given this information, I wouldn't think that the termination of the main thread is going to affect the shared_ptr in the worker thread.

What is the lifetime of a static variable in a C++ function?

If a variable is declared as static in a function's scope it is only initialized once and retains its value between function calls. What exactly is its lifetime? When do its constructor and destructor get called?
void foo()
{
static string plonk = "When will I die?";
}
The lifetime of function static variables begins the first time[0] the program flow encounters the declaration and it ends at program termination. This means that the run-time must perform some book keeping in order to destruct it only if it was actually constructed.
Additionally, since the standard says that the destructors of static objects must run in the reverse order of the completion of their construction[1], and the order of construction may depend on the specific program run, the order of construction must be taken into account.
Example
struct emitter {
string str;
emitter(const string& s) : str(s) { cout << "Created " << str << endl; }
~emitter() { cout << "Destroyed " << str << endl; }
};
void foo(bool skip_first)
{
if (!skip_first)
static emitter a("in if");
static emitter b("in foo");
}
int main(int argc, char*[])
{
foo(argc != 2);
if (argc == 3)
foo(false);
}
Output:
C:>sample.exe
Created in foo
Destroyed in foo
C:>sample.exe 1
Created in if
Created in foo
Destroyed in foo
Destroyed in if
C:>sample.exe 1 2
Created in foo
Created in if
Destroyed in if
Destroyed in foo
[0] Since C++98[2] has no reference to multiple threads how this will be behave in a multi-threaded environment is unspecified, and can be problematic as Roddy mentions.
[1] C++98 section 3.6.3.1 [basic.start.term]
[2] In C++11 statics are initialized in a thread safe way, this is also known as Magic Statics.
Motti is right about the order, but there are some other things to consider:
Compilers typically use a hidden flag variable to indicate if the local statics have already been initialized, and this flag is checked on every entry to the function. Obviously this is a small performance hit, but what's more of a concern is that this flag is not guaranteed to be thread-safe.
If you have a local static as above, and foo is called from multiple threads, you may have race conditions causing plonk to be initialized incorrectly or even multiple times. Also, in this case plonk may get destructed by a different thread than the one which constructed it.
Despite what the standard says, I'd be very wary of the actual order of local static destruction, because it's possible that you may unwittingly rely on a static being still valid after it's been destructed, and this is really difficult to track down.
The existing explanations aren't really complete without the actual rule from the Standard, found in 6.7:
The zero-initialization of all block-scope variables with static storage duration or thread storage duration is performed before any other initialization takes place. Constant initialization of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered. An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope. Otherwise such a variable is initialized the first time control passes through its declaration; such a variable 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 enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined.
FWIW, Codegear C++Builder doesn't destruct in the expected order according to the standard.
C:\> sample.exe 1 2
Created in foo
Created in if
Destroyed in foo
Destroyed in if
... which is another reason not to rely on the destruction order!
The Static variables are come into play once the program execution starts and it remain available till the program execution ends.
The Static variables are created in the Data Segment of the Memory.