简体   繁体   English

Xerces线程安全吗?

[英]Xerces thread safety?

taken from here: http://xerces.apache.org/xerces-c/faq-parse-3.html#faq-6 取自此处: 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. 也就是说,在包含两个解析器和两个线程的应用程序中,一个解析器可以在第一个线程内同时运行,而第二个解析器可以在第二个线程内运行。

But the below code fails whenever the QMutex is commented and does not whenever it is used. 但是,以下代码无论何时注释QMutex都会失败,并且不会在每次使用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;
}

What am I missing ? 我想念什么?

The functions get executed hundreds of times and at some point, I'm getting a segfault from either: 这些函数被执行了数百次,并且在某个时刻,我从以下任一处得到段错误:

XercesDOMParser* parser = new XercesDOMParser();

or 要么

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

A quote from the same FAQ answer you refer to: 来自您引用的同一常见问题解答答案的引文:

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. 应用程序还需要保证XMLPlatformUtils :: Initialize()和XMLPlatformUtils :: Terminate()方法是从同一线程(通常是执行main()的初始线程)中调用的,如果调用了多个线程,则应用程序将执行正确的同步XMLPlatformUtils :: Initialize()和XMLPlatformUtils :: Terminate()同时进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM