static-initialization

Thread-safety of static initializers in C#

丶灬走出姿态 提交于 2019-12-19 19:04:14
问题 Everyone says static initializers are thread-safe, but I'm worried about a particular detail. Let's say I have static class MyStaticClass { public static readonly object myField = MyOtherClass.GetNewObject(); } static class MyOtherClass { public static object GetNewObject() { /* arbitrary code that returns a new object */ } } Which of the following does C# guarantee, when MyStaticClass.myField is not yet initialized? If threads 1 and 2 try to access myField together (in that order),

Thread-safety of static initializers in C#

三世轮回 提交于 2019-12-19 19:03:58
问题 Everyone says static initializers are thread-safe, but I'm worried about a particular detail. Let's say I have static class MyStaticClass { public static readonly object myField = MyOtherClass.GetNewObject(); } static class MyOtherClass { public static object GetNewObject() { /* arbitrary code that returns a new object */ } } Which of the following does C# guarantee, when MyStaticClass.myField is not yet initialized? If threads 1 and 2 try to access myField together (in that order),

How to comprehend that an implementation is permitted to treat dynamic initialization of non-local variable as static initialization in some cases?

浪子不回头ぞ 提交于 2019-12-19 09:21:39
问题 In fact, the problem comes from the words in the standard draft N4582: [basic.start.static/3] An implementation is permitted to perform the initialization of a variable with static or thread storage duration as a static initialization even if such initialization is not required to be done statically, provided that — the dynamic version of the initialization does not change the value of any other object of static or thread storage duration prior to its initialization, and — the static version

Why using parallel streams in static initializer leads to not stable deadlock

戏子无情 提交于 2019-12-18 10:59:27
问题 CAUTION: it is not a duplicate, please read topic сarefully https://stackoverflow.com/users/3448419/apangin quote: The real question is why the code sometimes works when it should not. The issue reproduces even without lambdas. This makes me think there might be a JVM bug. In the comments of https://stackoverflow.com/a/53709217/2674303 I tried to find out reasons why code behaves differently from one start to another and participants of that discussion made me piece of of advice to create a

How can I run a static initializer method in C# before the Main() method?

Deadly 提交于 2019-12-18 07:30:21
问题 Given a static class with an initializer method: public static class Foo { // Class members... internal static init() { // Do some initialization... } } How can I ensure the initializer is run before Main() ? The best I can think of is to add this to Foo : private class Initializer { private static bool isDone = false; public Initializer() { if (!isDone) { init(); isDone = true; } } } private static readonly Initializer initializer = new Initializer(); Will this work or are there some

g++, static initialization and -nostdlib

蓝咒 提交于 2019-12-18 02:42:40
问题 Compiling / linking with -nostdlib seems to prevent static initialization, even if I add my own crti.s and crtn.s with .init / .fini sections. Are there workarounds to make g++ generate static initialization code that is inserted in .init or that I can call manually? This is what I tried: g++ -o test.o -c -fno-use-cxa-atexit test.cc # has _start (entry point) # that calls _init and _main as -o crti.o crti.s # has _init in section .init as -o crtn.o crtn.s g++ -o test ./crti.o test.o

Initialization order of static data inside class template

蓝咒 提交于 2019-12-17 17:09:51
问题 // File: InitFirst.h #pragma once template <int val> struct InitFirst { static float s_dividedByThree; }; template <int val> float InitFirst<val>::s_dividedByThree = val / 3.0; // File: Test.h #include <conio.h> #include <tchar.h> #include "InitFirst.h" float g_shouldBeOneThird = InitFirst<1>::s_dividedByThree; int _tmain(int argc, _TCHAR* argv[]) { _cprintf("%f\n", g_shouldBeOneThird); getch(); return 0; } Is g_shouldBeOneThird guaranteed to be initialized to around 0.333? In other words, is

Static initialization order issue in C++

落花浮王杯 提交于 2019-12-13 05:17:05
问题 This is another variation of an old theme: The initialization order of static objects in different translation units is not defined. Below is a stripped-down example of my particular scenario. The classes G and F are non-POD types. F depends on G is the sense that to construct an instance of F you need some number of instances of G. (For example, F could be some message an application emits, and instances of G would be components of such messages.) G.hpp #ifndef G_HPP #define G_HPP struct G {

Cost of thread-safe local static variable initialization in C++11?

你说的曾经没有我的故事 提交于 2019-12-13 02:25:04
问题 We know that local static variable initialization is thread-safe in C++11, and modern compilers fully support this. (Is local static variable initialization thread-safe in C++11?) What is the cost of making it thread-safe? I understand that this could very well be compiler implementation dependent. Context: I have a multi-threaded application (10 threads) accessing a singleton object pool instance via the following function at very high rates, and I'm concerned about its performance

Can “construct on first use” idiom fail under any circumstances?

时光总嘲笑我的痴心妄想 提交于 2019-12-12 08:10:16
问题 I'm building my program (tests actually) using some static library. This library contains one file inside which I have functions like that: string& GetString() { static string strFilename; return strFilename; } void PrintToScreen() { printf("String: %s\n", GetString().c_str()) } Then in my main.cpp (outside the library) I'm doing: GetString() = "abc"; printf("String: %s\n", GetString().c_str()); PrintToScreen(); And I get this output: String: abc String: So looks like second call to the