lundi 24 mai 2021

Is call xmlParserInit() from lambda function called to initialize static variable a bad idea?

These are constrains:

  1. I need use libxml2 SAX interface in multithreaded environment
  2. Code is statically linked
  3. I don't have control on threads, so I don't have a direct access to main thread from which xmlParserInit() should be called in accordance to documentation

I came up with the following idea

// SAXClient.cpp

const static bool PARSER_INITIALIZED = ()[] {
    xmlParserInit();
    return true;
}();

This code initializes the variable by the lambda called even prior to main() call in the main thread as it is required.

Do I miss some pitfall here?

Aucun commentaire:

Enregistrer un commentaire