Re: [xml] threads and xmlInitParser

On Sun, Sep 14, 2003 at 12:05:33PM +0200, Fredrik Arnerup wrote:

I had a threading related problem with the saxparser that I managed to
resolve by putting a call to xmlInitParser() in main().
However, the problem also disappeared when I put a call to xmlInitParser()
inside each thread before starting the parser.

I realize this is not what the docs say I should do, but can I expect
xmlInitParser() to work even if it is called from a separate thread and can
I call it more than once?

static int xmlParserInitialized = 0;

 * xmlInitParser:
 * Initialization function for the XML parser.
 * This is not reentrant. Call once before processing in case of
 * use in multithreaded programs.
xmlInitParser(void) {
    if (xmlParserInitialized != 0)

  I think it's pretty clear that calling it in concurrent threads while
not initialized introduce a hazard. Don't do that ! Moreover the function
doc says so too so really don't do that !!!
  Once initialized you can call it to your heart content until
someone calls xmlCleanupParser() which no thread should ever do.

  This allocates memory, including parser entities, make sure it's called
at the beginning in a thread aware application, and call xmlCleanupParser()
only when nobody uses the library or XML documents anymore, and if
you can't be sure do not call it.

I ask partly for my own sake and partly because I hope to produce a patch
that would make libxml++ more thread safe.

  The very basis for a library code would be to conform the way the
API intends to be used i.e. as documented. Again call it at init time !


Daniel Veillard      | Red Hat Network
veillard redhat com  | libxml GNOME XML XSLT toolkit | Rpmfind RPM search engine

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]