是否可以将对象实例移至QT项目中不同代码点的不同线程
Is it possible to move an object instance to different threads at different points of code in a QT project?
我正在尝试确定在运行时是否可以将一个对象实例移至不同点的不同线程。
以下是一些示例代码,可以向您展示我的意思:
this->thread1 = new QThread( this );
this->thread2 = new QThread( this );
this->pObject->moveToThread( this->thread1 );
connect(this->thread1, SIGNAL(started()), this->pObject, SLOT(fnc1()));
connect(this->thread2, SIGNAL(started()), this->pObject, SLOT(fnc2()));
this->thread1->start();
//after thread1 has finished
this->pObject->moveToThread( this->thread2 );
this->thread2->start();
可以做到吗?
编辑:在Kuba关于不使用直接连接的建议之后,他指出我必须以某种方式干扰事件循环之后,我意识到手动终止线程不是一个好主意。我在此处添加了对线程的终止,以显示我出错的位置,并尝试找到一种更好的实现结果的方法。
connect(this->pObject, SIGNAL(finished()), this, SLOT(stopThread()));
void Class::stopThread( void )
{
if( this->thread1->isRunning() )
{
this->thread1->terminate();
return;
}
if( this->thread2->isRunning() )
{
this->thread2->terminate();
this->pObject->moveToThread( this->thread1 );
}
}
void Object::fnc1( void )
{
/*Does some work..*/
finished(); //Calls 'finished' to signal stopThread when done (not stopping on its own)
}
附加信息我有主流将实例保存到thread1,thread2和pobject(指向对象的指针我要从thread1转到thread2,并在必要时再次返回)。
主类构造函数:
MainClass::MainClass( QWidget *parent ) : QMainWindow(parent)
{
this->ui.setupUi(this);
this->thread1 = new QThread( this );
this->thread2 = new QThread( this );
this->pObject->moveToThread( this->thread1 );
connect( this->pObject, SIGNAL(finished()), this, SLOT(stopThread()) );
connect( this->thread1, SIGNAL(started()), this->pObject, SLOT(fnc1()) );
connect( this->thread2, SIGNAL(started()), this->pObject, SLOT(fnc2()) );
}
单击菜单项时的插槽:
void MainClass::on_action_call_fnc1_triggered( void )
{
if( this->thread1->isRunning() )
return;
/*EXECUTES SOME CLASSIFIED CODE THAT CANNOT BE SHOWN*/
this->thread1->start();//should trigger fnc1 execution
}
fnc1在thread1启动时称为对象类中的fnc1:
void Object::fnc1( void )
{
/*DOES SOME MORE SECRET PROCESSING THAT CANNOT BE SHOWN*/
this->finished(); // triggers MainClass::stopThread( void )
}
单击菜单项时的插槽开始执行FNC2:
void MainClass::on_action_call_fnc2_triggered( void )
{
if( this->thread1->isRunning() || this->thread2->isRunning() )
return;
this->pObject->moveToThread( this->thread2 );
this->thread2->start();//should trigger fnc2 execution
}
fnc2保存在对象中:
void Object::fnc2( void )
{
/*DOES SOME MORE SECRET PROCESSING THAT CANNOT BE SHOWN*/
this->finished(); // triggers MainClass::stopThread( void )
}
止损功能:
void MainClass::stopThread( void )
{
if( this->thread1->isRunning() )
{
/*this->thread1->quit();
this->thread1->exit();*/
this->thread1->terminate();
//this->thread1->wait( 0 ); //Trying different ways of stopping the thread
return;
}
if( this->thread2->isRunning() )
{
this->thread2->terminate();
//this->thread2->quit(); // Trying different ways of stopping the thread
//this->thread2->exit();
this->pObject->moveToThread( this->thread1 );
}
}
它将起作用,但是您必须断言对象的线程确实已经完成:
Q_ASSERT(pObject->thread() == nullptr);
pObject->moveToThread(thread2);
thread1
完成后,对象的线程变为null,只有这样,您才可以将其从任意线程移动到另一个线程。否则,如果对象的线程尚未完成,则只能将对象从其线程移动:
QTimer::singleShot(0, pObject, [this]{ pObject->moveToThread(thread2); }
相关文章:
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- ASIO signal_set多个 IO 线程不可靠,具体取决于代码顺序?
- 如何分析代码的哪一部分创建了线程?
- 如何完全关闭 TBB 代码中的线程
- 提供对不同类型的数据(建议、代码审查)的线程安全访问的类
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- C++11:具有互斥锁的线程看到原子变量的值发生变化,尽管这是唯一可以改变它的代码
- 琐碎并发代码的吞吐量不会随着线程数量的增加而增加
- 删除原子多线程代码中的容器
- Visual Studio 发布模式阻止在调试模式下执行的代码.使用 WinHTTP 和多线程
- 开始多线程代码之前的初始化
- 使用 std::chrono::steady_clock 对线程/异步中的代码进行基准测试
- 实现在多线程代码中安全恢复的断点
- 一个简单的 win32 多线程代码.这能行吗?
- 终止调用本机代码的 .Net 线程
- 线程 1:xcode 中出现EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)错误
- 有没有更好的方法可以使此代码线程安全?线程局部静态似乎是一个生硬的工具
- 如何使用 openMP 使此代码线程安全
- ls 此代码线程安全
- 如何使这个代码线程安全?ID + +;Print_Value (ID);