Archived

This forum has been archived. Please start a new discussion on GitHub.

c++: static_id as global variables and undeterministic creation order

Hello,

we are creating a hashValue of the ice_staticId() of Slice-generated classes and assign them to global Variables. This function (ice_staticId() ) relies on global variables. The creation order of such global variables is undefined. So it can haben, that our variable may be created before the variable behind ice_staticId().

This could be avoided if the definition of the id-string is in a static variable in a function and then returned. This way it would be assured that the variable is always created when used.

regards

Comments

  • xdm
    xdm La Coruña, Spain
    We will discuss if there is a better way to initialize ice_staticId

    The solution you proposed doesn't work for Ice because is not thread safe, see C++ scoped static initialization is not thread-safe, on purpose! - The Old New Thing - Site Home - MSDN Blogs.

    I think the simple workaround is to move the initialization of your hash to be done after static initialization has happened.
  • You could assure that the function is called, when the program is initializing and for sure single-threaded (?), if you assign the return value of the function to a global variable right now.
    Then it is like now, only that other variables can use the static_id as well.
  • xdm
    xdm La Coruña, Spain
    You could assure that the function is called, when the program is initializing and for sure single-threaded (?),

    We can add code that call all ice_staticId functions during static initialization (at this point all is singled thread) but this will add some overhead to static initialization. We will discuss internally how to better fix that.

    Thanks for pointing this issue
  • Don't know if it is a missunderstanding, but i meant a construct like this:
    static const std::string& ::some_namespace::some_class_name::ice_staticid(){
       static std::string static_id = "::some_namespace::some_class_name";
       return static_id;
    }
    const std::string __some_namespace__some_class_name = ::some_namespace::some_class_name::ice_staticid();
    

    regards.