libxml2.so Init/CleanupParser 多线程线程的用法

libxml2.so Init/CleanupParser usage for multiple processes with threads

我正在使用 libxml2 作为 共享库 来自 上不同的并行 运行ning 进程arm/linux环境。官方示例是一次性流程,其中 InitParser() and CleanupParser() 的处理很简单。

在多个进程使用 libxml2 的设置中,我不确定如何使用它。

proc1 -thread1-> work -> libxml2.so -> ...
      -thread2-> work -> ...
      -thread3-> work -> work -> libxml2.so -> work -> ...
proc2 -thread1-> wrapper.so -> libxml2.so -> work -> ...
      -thread2-> work -> ...
      -thread3-> wrapper.so -> libxml2.so -> ...

问题


InitParser()

Initialization function for the XML parser. This is not reentrant. Call once before processing in case of use in multithreaded programs

CleanupParser()

it cleans up memory allocated by the library itself. It is a cleanup function for the XML library. It tries to reclaim all related global memory allocated for the library processing. It doesn't deallocate any document related memory. One should call xmlCleanupParser() only when the process has finished using the library and all XML/HTML documents built with it. See also xmlInitParser() which has the opposite function of preparing the library for operations. WARNING: if your application is multithreaded or has plugin support calling this may crash the application if another thread or a plugin is still using libxml2

我想,这个问题揭示了我对共享库内部工作的有限了解...感谢您的帮助!

xmlInitParser 不是线程安全的。最好在生成其他线程之前从主线程调用此函数一次,例如,在 main.

的开头 如果进程的任何线程之后调用任何其他 libxml2 函数,则绝不能调用

xmlCleanupParser。所以 xmlInitParser() ... xmlCleanupParser() 在线程级别上是不行的。您通常根本不需要调用 xmlCleanupParser,但在使用 valgrind 或 ASan 等工具检查内存泄漏时,您需要它来避免误报。在这种情况下,最好在进程退出之前调用它,例如在 main.

的末尾