在我的Qt应用程序中,我创建了一个QThread应该定期执行一些繁重的计算任务.主QApplication线程应该维护GUI(不包括在示例中)并执行一些定期更新.两个线程都有自己的定时器来启用常规update()调用.
问题:当工作线程的计算工作负载超过某个临界值时,我的主线程停止接收计时器事件.
示例代码如下.当主线程调用update()时,它输出"Main",工作线程调用"Worker".如果你运行它,你会看到"工人"定期打印,"主"正好出现两次(一个在开头,一个在约5秒).在全功能GUI应用程序的情况下,这实际上意味着完全GUI冻结.
一些观察.
所以,你可以看到我有几个解决方法,但我很感激任何人解释我的原始代码有什么问题.我希望线程能够独立地执行它们的事件循环.我知道我通过一个很长的update()操作来阻止工作线程事件循环,但为什么它在世界上会影响主线程呢?
PS是的,我知道QConcurrent替代方案.但我只是想明白.
#include <windows.h>
#include <QApplication>
#include "test.h"
HANDLE mainThread_ = INVALID_HANDLE_VALUE;
QApplication *app_ = 0;
MyObj *obj_ = 0;
MyThread *thread_ = 0;
MyObj::MyObj()
: timer_(0)
{
timer_ = new QTimer(0);
connect(timer_, SIGNAL(timeout()), this, SLOT(update()));
timer_->start(10);
}
void MyObj::update()
{
printf("Main\n");
}
void MyThread::run()
{
timer_ = new QTimer(0);
connect(timer_, SIGNAL(timeout()), this, SLOT(update()));
timer_->start(10);
exec();
}
void MyThread::update()
{
printf("Worker\n");
// do some …Run Code Online (Sandbox Code Playgroud) 可能我只是无法掌握本体背后的一些基本思想,但这是我的问题.我正在尝试从RDF存储中提取三元组(使用4store,但也尝试使用XML ArmyKnife),并使用SPARQL查询指定谓词并获取空结果.
为了确保我没有使用RDF语法弄乱任何东西,我使用LUBM生成的数据(剥离到适合的示例大小).
<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF
xml:base = "http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl"
xmlns:rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:ub="univ-bench.owl#"
>
<owl:DatatypeProperty rdf:ID="name">
<rdfs:label>name</rdfs:label>
</owl:DatatypeProperty>
<owl:Class rdf:ID="Organization">
<rdfs:label>organization</rdfs:label>
</owl:Class>
<owl:Class rdf:ID="University">
<rdfs:label>university</rdfs:label>
<rdfs:subClassOf rdf:resource="#Organization" />
</owl:Class>
<ub:University rdf:about="http://www.University0.edu">
<ub:name>University0</ub:name>
</ub:University>
</rdf:RDF>
Run Code Online (Sandbox Code Playgroud)
然后我运行一个查询来查看导入后我的数据库实际包含的三元组:
SELECT * WHERE {?s ?p ?o} ORDERBY ?s
Run Code Online (Sandbox Code Playgroud)
这是结果:
<http://www.University0.edu> <univ-bench.owl#name> "University0"
<http://www.University0.edu> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <univ-bench.owl#University>
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#Organization> <http://www.w3.org/2000/01/rdf-schema#label> "organization"
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#Organization> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#University> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#Organization>
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#University> <http://www.w3.org/2000/01/rdf-schema#label> "university"
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#University> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#name> <http://www.w3.org/2000/01/rdf-schema#label> "name"
<http://www.lehigh.edu/~zhp2/2004/0401/univ-bench.owl#name> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#DatatypeProperty>
Run Code Online (Sandbox Code Playgroud)
很明显,我将<univ-bench.owl#name>谓词作为第一个三元组的一部分.
然而,以下查询返回没有结果:
SELECT …Run Code Online (Sandbox Code Playgroud)