首页| 论坛| 消息

标题:子线程启动、关闭、再启动的问题
作者:weiweiqiao
日期:2021-06-07 18:34
内容:

我在读取数据时,希望在子线程中读取,不要影响到ui用户体验,当读取完数据后,将子线程关闭,减少资源消耗。
现有代码如下,希望大佬指点。
启动子线程:
void Ui_process::start_sub_thread(QThread &thread, QObject *process)
{
if ( !thread.isRunning()) {
if (nullptr != process) {
// Move object into sub thread. create signal/slot
process->moveToThread(&thread);
//connect(&thread, &QThread::finished, process, &QObject::deleteLater);

thread.start();
}
}
}当子线程处理完数据,将处理对象移出子线程:
void Ui_process::sl_quit_sub_thread(int index)
{
if ( file_thread_.isRunning()) {
qDebug()thread()->currentThread());
//file_process_->setParent(nullptr);
disconnect(&file_thread_, nullptr, file_process_, nullptr);

qDebug()


#1 [yuyu414 06-08 16:39]
Warning: This function is not thread-safe; the current thread must be same as the current thread affinity. In other words, this function can only "push" an object from the current thread to another thread, it cannot "pull" an object from any arbitrary thread to the current thread. There is one exception to this rule however: objects with no thread affinity can be "pulled" to the current thread.
#2 [yuyu414 06-08 16:40]
没有必要,线程空跑并不会占用资源,你的需求用QThreadPool更合适
#3 回 yuyu414 的帖子 [weiweiqiao 06-09 16:03]
yuyu414:没有必要,线程空跑并不会占用资源,你的需求用QThreadPool更合适 (2021-06-08 16:40) 
好的,谢谢,我试试
#4 [九重水 06-10 09:55]
我的子线程好像没关过,都是在等‘事务’进来,有就处理,没就干等着。
#5 回 九重水 的帖子 [weiweiqiao 06-10 16:14]
九重水:我的子线程好像没关过,都是在等‘事务’进来,有就处理,没就干等着。  (2021-06-10 09:55) 
哈哈,我原来也是放着不管,现在想试试看多次启动有啥情况~

回复 发表
主题 版块