如何检查libxml2是否已初始化 [英] How to check if libxml2 is initialized

查看:250
本文介绍了如何检查libxml2是否已初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据关于多线程的文档,必须在使用该库之前调用xmlInitParser()同时.这是问题所在.我使用libxml2编码了一个库(不是可执行文件),它应该保证并发性.因此,我决定在库的init函数上调用init函数.但是,文档表示该函数不可重入.因此,如果其他库或链接我的库的程序事先调用了该函数,则可能会出现问题.

According to this document about multithreading, it is mandatory to call xmlInitParser() before using the library concurrently. Here's the problem. I coded a library(not an executable) using libxml2 and it should guarantee concurrency. So I decided to call the init function on my library's init function. However, the document says the function is not reentrant. So it could be problematic if other libraries or the program linking my library calls the function beforehand.

我找不到检查解析器(或我应该说libxml2)是否已初始化的函数或方法.我该怎么办?不管是否调用该函数,并希望获得最好的?我将在发布此函数后测试该函数是否毕竟是可重入的,但这并不是真的.

I couldn't find a function or a way to check if the parser(or libxml2 should I say) is initialised. What should I do? Call the function regardless and hope for the best? I'm going to test if the function is reentrant after all, after I post this but that doesn't really tally.

为澄清起见,总结:

  • xmlInitParser()实际上是可重入的吗?
  • 有什么方法可以检查libxml2是否已初始化?
  • (OR)在另一个软件也可以同时使用libxml2的前提下,如何安全地同时使用libxml2.
  • Is xmlInitParser() actually reentrant?
  • Any way to check if libxml2 is initialised?
  • (OR) How to safely use libxml2 concurrently under the premise that another software could be using it concurrently as well.

推荐答案

查看源代码后(取自此处)似乎可以多次调用该函数:

After looking at the sourcecode (taken from here) it seems possible to call the function multiple times:

static int xmlParserInitialized = 0;

void
xmlInitParser(void) {
    if (xmlParserInitialized != 0)
     return;

 #ifdef LIBXML_THREAD_ENABLED
     __xmlGlobalInitMutexLock();
     if (xmlParserInitialized == 0) {
 #endif
     /* ... the actual initialization ... */
     xmlParserInitialized = 1;
 #ifdef LIBXML_THREAD_ENABLED
     }
     __xmlGlobalInitMutexUnlock();
 #endif
 }

您担心其他多个库同时调用xmlInitParser(). System V ABI意味着将一个接一个地加载库(请参阅初始化和终止函数"一节).
[假设没有其他库创建线程(调用xmlInitParser())],这意味着您不必担心.

You are concerned about multiple other libraries calling xmlInitParser() concurrently. The System V ABI implies that libraries are loaded one after another (see the section "Initialization and Termination Functions").
[Assuming none of the other libraries creates threads (that call xmlInitParser())] this means that you do not have to worry about it.

如果您确实希望安全,则应在库中静态链接libxml,这样您便拥有了自己的私有副本,而其他库也无法干预.

If you really want to be safe you should link libxml statically in your library, so you have your own private copy that other libraries cannot interfere with.

这篇关于如何检查libxml2是否已初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆