- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 main() 中创建了一个 QThread,即主线程。将 worker 类移动到新线程。该线程执行工作类的“StartThread”方法。
工作线程:
//header file
class Worker : public QObject
{
Q_OBJECT
public:
Worker(QThread* thread);
public slots:
void StartThread();
void EndThread();
private:
QThread* m_pCurrentThread;
};
// source file
#include "QDebug"
#include "QThread"
#include "worker.h"
Worker::Worker(QThread* thread)
{
m_pCurrentThread = thread;
}
void Worker::StartThread()
{
qDebug() << " StartThread";
while(true)
{
QThread::msleep(1000);
qDebug() << " thread running";
static int count = 0;
count++;
if(count == 10)
{
break;
}
if(m_pCurrentThread->isInterruptionRequested())
{
qDebug() << " is interrupt requested";
// Option 3:
m_pCurrentThread->exit();
}
}
qDebug() << "StartThread finished";
}
void Worker::EndThread()
{
qDebug() << "thread finished";
}
main.cpp
#include <QCoreApplication>
#include "worker.h"
#include "QThread"
#include "QObject"
#include "QDebug"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QThread* thread = new QThread();
Worker* workerObj = new Worker(thread);
QObject::connect(thread,
SIGNAL(started()),
workerObj,
SLOT(StartThread()));
QObject::connect(thread,
SIGNAL(finished()),
workerObj,
SLOT(EndThread()));
workerObj->moveToThread(thread);
thread->start();
thread->requestInterruption();
QThread::msleep(2000);
qDebug() << "terminate thread";
if(thread->isRunning())
{
// Option 1,2 exit() / quit() used but got same output
thread->exit();
qDebug() << "wait on thread";
thread->wait();
}
qDebug() << " exiting main";
return a.exec();
}
现在,在“StartThread”完成并且线程正常退出之前,我想从主线程中停止该线程。用过,
thread->quit()并在主线程中等待 (thread->wait())。
“StartThread”中的 exit()
预期: 一旦从主线程调用 exit()/quit(),线程执行应该立即停止。
实际: 在所有三种情况下,线程都保持运行,完成“StartThread”方法并正常退出。与不调用 exit()/quit() 一样好。 输出:
StartThread
thread running
is interrupt requested
terminate thread
wait on thread
thread running
is interrupt requested
thread running
is interrupt requested
thread running
is interrupt requested
thread running
is interrupt requested
thread running
is interrupt requested
thread running
is interrupt requested
thread running
is interrupt requested
thread running
is interrupt requested
thread running
StartThread finished
thread finished
exiting main
是否可以在主线程需要时停止/处置线程?
最佳答案
isInterruptionRequested
仅当您调用了 QThread::requestInterruption
时才返回 true,不是 QThread::quit
.这已记录在案。
如果你想使用QThread::quit
,线程必须旋转一个事件循环,所以你不应该从它派生;而是这样做:
class Worker : public QObject {
QBasicTimer m_timer;
int chunksToDo = 20;
void doChunkOfWork() {
QThread::sleep(1);
}
void timerEvent(QTimerEvent * ev) {
if (ev->timerId() != m_timer.timerId()) return;
if (!chunksToDo) { m_timer.stop(); return; }
doChunkOfWork();
if (!--chunksToDo) emit done();
}
public:
explicit Worker(QObject * parent = nullptr) : QObject{parent} {
m_timer.start(0, this);
}
Q_SIGNAL void done();
};
要运行 worker,请创建它并将其移动到它的某个线程。要停止 worker,只需在 worker 完成之前 quit()
它的线程。
int main(int argc, char ** argv) {
QCoreApplication app{argc, argv};
Worker worker;
QThread thread;
worker.moveToThread(&thread);
QObject::connect(&worker, &Worker::done, &thread, &QThread::quit);
QObject::connect(&thread, &QThread::finished, &app, &QCoreApplication::quit);
thread.start();
return app.exec();
}
您可能希望使用线程池,而不是手动管理线程的生命周期。
class Worker : public QObject, QRunnable {
int chunksToDo = 20;
volatile bool active = true;
void doChunkOfWork() {
QThread::sleep(1);
}
void run() {
while (active && chunksToDo) {
doChunkOfWork();
--chunksToDo;
}
emit done();
}
public:
using QObject::QObject;
Q_SLOT void stop() { active = false; }
Q_SIGNAL void done();
};
int main(int argc, char ** argv) {
QCoreApplication app{argc, argv};
QThreadPool pool;
Worker worker;
worker.setAutoDelete(false); // important!
pool.start(&worker);
// *
QTimer::singleShot(5000, &worker, &Worker::stop);
QObject::connect(&worker, &Worker::done, &app, &QCoreApplication::quit);
return app.exec();
}
在不运行主事件循环的情况下结束它的另一种方法是:
// *
QThread::sleep(5);
worker.stop();
pool.waitForDone();
}
This answer有一个将多个作业流式传输到线程池的完整示例。
关于c++ - 停止 Qt 线程 : calling exit() or quit() does not stop the thread execution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38022167/
我在stackoverflow上查过很多类似的问题,比如call.call 1 , call.call 2 ,但我是新人,无法发表任何评论。我希望我能找到关于 JavaScript 解释器如何执行这些
“strace 是一个系统调用跟踪器,即一个调试工具,它打印出另一个进程/程序进行的所有系统调用的跟踪。”如果系统调用递归工作或一个系统调用调用另一个系统调用怎么办。我怎样才能得到这些信息? 可能的解
我的问题很简单:我正在将一个函数传递给其他一些稍后调用的函数(示例回调函数),问题是何时、为何以及最佳做法是什么。 样本:我有 xxx() 函数,我必须传递它,如下面的 window.onload 事
我是 Java 新手,我正在尝试学习 ScheduledExecutorService 接口(interface)。我在网上看到了下面的代码。 我没有看到任何对 Callable.call() 方法的
这是我的调用过程: System.out.println
在 typescript 中,我有一个 DataAccess 类,以便所有 Ajax 调用都通过单个对象进行路由,以节省应用程序中许多地方的代码重复。 在使用这种方法时,我需要使用回调将响应返回到调用
如何使用模拟来计算通过 call 或 apply 进行的函数调用 // mylib.js module.exports = { requestInfo: function(model, id) {
每次我尝试roxygenize 一个包我都会得到这个错误: Error: is.call(call) is not TRUE traceback() 的结果: 11: stop(sprintf(nge
这里如果我有一个记录“调用我的函数”的函数 function myFunction() { console.log('called my function') } Function.prototy
在 Javascript 中,Function.call() 可以在给定 this 值和零个或多个参数的情况下调用 Function。 Function.call 本身就是一个函数。所以理论上,Fun
这个问题已经有答案了: "object is not a function" when saving function.call to a variable (3 个回答) a is a functi
在调用 UITableView 上的 reloadData 方法后,我曾多次遇到此问题,但我不明白为什么? 这是一个问题,因为如果更新 TableView 的数据,tableview将不必要地查询不存
我继承了大约 400 行写得非常奇怪的 Fortran 77 代码,我正在尝试逐步分析它以使其在我的脑海中清晰。 无论如何,我有一个类似 header 的文件(实际上是一个 .h,但其中的代码是 fo
这是我的代码 class AuthAction(callbackUri:String) extends ActionBuilder[UserRequest] with ActionRefiner[
我继承了大约 400 行写得非常奇怪的 Fortran 77 代码,我正在尝试逐步分析它以使其在我的脑海中清晰。 无论如何,我有一个类似 header 的文件(实际上是一个 .h,但其中的代码是 fo
我知道这个问题之前在这里被问过 iOS 6 shouldAutorotate: is NOT being called .但我的情况有点不同。 最初,在应用程序启动时,我加载了一个 viewContr
我是 headfirst 设计模式的读者,我注意到了这一点。 “好莱坞原则,别叫我们,我们叫你” 这意味着高级组件告诉低级组件“不要调用我们,我们调用你” High-Level Component 是
这个问题在这里已经有了答案: Why does passing variables to subprocess.Popen not work despite passing a list of ar
我刚找到一个覆盖 OnPaintBackground 的表单。奇怪的是它从来没有被调用过!就像,完全一样。为什么是这样?表单被刷新、移动、调整大小等等,所以它应该一些重新绘制,对吧? 最佳答案 是否设
调用函数的方式 考虑这个简单的函数: function my(p) { console.log(p) } 我可以这样调用它: my("Hello"); 也像这样: my.call(this, "Hel
我是一名优秀的程序员,十分优秀!