c++ - Xerces线程安全?

标签 c++ multithreading xerces

取自这里: http://xerces.apache.org/xerces-c/faq-parse-3.html#faq-6

When two or more parser instances exist in a process, the instances can be used concurrently, without external synchronization. That is, in an application containing two parsers and two threads, one parser can be running within the first thread concurrently with the second parser running within the second thread.

但是下面的代码会在 QMutex 被注释时失败,而在被使用时不会失败。

bool reports::validateSchema( QString fileName )
{
    // QMutexLocker lock( &xercesMutex );
    try
    {
        XMLPlatformUtils::Initialize();
    }
    catch(...)
    {
        this->throw_report_exception(__FILE__,__LINE__,__TIME__,__DATE__,"reports::validateSchema",
                                     "unable to initialize Xerces Plateform");
        return false;
    }


    const char* const xsd = "full absloute path to .xsd ==> hard written";

    XercesDOMParser* parser = new XercesDOMParser();

    try
    {
        parser->setValidationSchemaFullChecking(true);
        parser->setDoSchema(true);
        parser->setDoNamespaces(true);
        parser->setValidationConstraintFatal(true);
        parser->setValidationScheme(XercesDOMParser::Val_Auto);

        ParserErrorHandler errHandler;
        parser->setErrorHandler(&errHandler);

        parser->cacheGrammarFromParse(true);
        parser->loadGrammar(xsd,Grammar::SchemaGrammarType,true);


        parser->parse(fileName.toStdString().c_str());
        std::cout << parser->getErrorCount() << std::endl;
        if(parser->getErrorCount()!=0)
        {
            return false;
        }
    }
    catch (const XMLException& toCatch)
    {
        char* message = XMLString::transcode(toCatch.getMessage());
        std::cout << "Exception message is: \n"
                << message << "\n";
        XMLString::release(&message);
        return false;
    }
    catch (const DOMException& toCatch)
    {
        char* message = XMLString::transcode(toCatch.msg);
        std::cout << "Exception message is: \n"
                << message << "\n";
        XMLString::release(&message);
        return false;
    }
    catch (...)
    {
        std::cout << "Unexpected Exception \n" ;
        return false;
    }

    delete parser;
    XMLPlatformUtils::Terminate();


    return true;
}

我错过了什么?

这些函数被执行了数百次,并且在某个时候,我从其中一个中得到了一个段错误:

XercesDOMParser* parser = new XercesDOMParser();

parser->loadGrammar(xsd,Grammar::SchemaGrammarType,true);

最佳答案

您引用的相同常见问题解答中的引述:

The application also needs to guarantee that the XMLPlatformUtils::Initialize() and XMLPlatformUtils::Terminate() methods are called from the same thread (usually the initial thread executing main()) or proper synchronization is performed by the application if multiple threads call XMLPlatformUtils::Initialize() and XMLPlatformUtils::Terminate() concurrently.

关于c++ - Xerces线程安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19585080/

相关文章:

c++ - 按值将项目插入 vector 与 unique_ptr

multithreading - 在全局异常处理程序中处理线程异常?

multithreading - boost 线程异常处理

c++ - 使用 Xerces 验证未定义架构的 XML

xml - XS型号 : getting model group information

c++ - CMake FindXercesC.cmake 找不到我的 XercesC

c++ - Delphi 和 C++,dll 导入

c++ - 如何将函数变量传递给 C++ lambda 函数

c++ - boost_assert 一个参数类实现了某个方法

python - 在单独的线程中扭曲传输。