取自这里: 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/