C++ Static Initializer - Is it thread safe -


usually, when try initialize static variable

class test2 { public:     static vector<string> stringlist; private:     static bool __init;     static bool init() {         stringlist.push_back("string1");         stringlist.push_back("string2");         stringlist.push_back("string3");          return true;     } };  // implement vector<string> test2::stringlist; bool test2::__init = test2::init(); 
  1. is following code thread safe, during static variable initialization?
  2. is there better way static initialize stringlist, instead of using seperate static function (init)?

although initialization shall happen before main function (hence, there can no threads simultaneous access init), concern :

  1. i have exe application.
  2. my exe application load a.dll, b.dll , c.dll
  3. a/b/c.dll, in turn load common.dll. above code inside common.dll
  4. i had verify. since 3 dll within single process, referring same static variable (vector).
  5. in case, prevent 3 dlls simultaneous access init (can view them 3 threads? although doesn't make sense @ first thought), init function, shall use critical section protect it?

i using windows xp, vc6 , vc2008 compiler.

i asked similar question while back:

loadlibrary , static globals

when comes dlls, static initialization , call dllmain bracketed internal critical section, thread-safe. second thread wait until first done before loads dll.

so in short, static init safe.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -