Skip to content

Conversation

@lichaofan2008
Copy link
Contributor

When performing the destruction operation, it is necessary to wait for the child thread to exit to avoid errors in resource release.

fix: 关闭应用引起进程崩溃

析构操作时,需要等待子线程退出,避免资源释放出错。

Bug: https://pms.uniontech.com/bug-view-334545.html (参考v20)

When performing the destruction operation, it is necessary to wait for the child thread to exit to avoid errors in resource release.

fix: 关闭应用引起进程崩溃

析构操作时,需要等待子线程退出,避免资源释放出错。

Bug: https://pms.uniontech.com/bug-view-334545.html
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码展示了在析构函数中对工作线程的处理,我来分析一下它的优缺点和改进建议:

优点:

  1. 正确设置了线程结束标志 m_isEndThread
  2. 添加了线程安全退出的逻辑,包括请求中断、等待和超时处理
  3. 使用了 Qt 的推荐方式 requestInterruption() 来请求线程安全退出
  4. 设置了合理的超时时间(3秒)
  5. 使用了 deleteLater() 来安全删除线程对象
  6. 在超时情况下使用了 qWarning() 记录日志

改进建议:

  1. 代码组织和注释:
MainWidget::~MainWidget()
{
    // 1. 设置线程结束标志
    m_isEndThread = 0;
    
    // 2. 安全停止并清理工作线程
    if (m_loadImagethread) {
        // 请求线程安全退出
        m_loadImagethread->requestInterruption();
        
        // 等待线程结束,设置超时时间
        if (!m_loadImagethread->wait(3000)) {
            qWarning() << "OCR thread did not finish within 3 seconds, forcing termination";
            m_loadImagethread->terminate();
            m_loadImagethread->deleteLater();
            m_loadImagethread = nullptr;
        }
        // 正常退出情况下,线程对象已在槽函数中处理
    }
}
  1. 线程安全性改进:
  • 建议将 m_isEndThread 改为 std::atomic 类型,确保操作的原子性
  • 考虑添加互斥锁来保护对线程对象的访问
  1. 错误处理:
  • 可以考虑在 terminate() 之后添加错误检查
  • 建议添加更详细的日志记录,包括线程启动和停止的时间戳
  1. 性能考虑:
  • 3秒的超时时间可能需要根据实际应用场景调整
  • 考虑将超时时间定义为类常量,便于统一管理和修改
  1. 代码健壮性:
  • 建议检查线程对象的状态,确保它处于可运行状态
  • 考虑添加异常处理机制

改进后的代码示例:

// 在头文件中定义
constexpr int THREAD_TIMEOUT_MS = 3000; // 线程超时时间(毫秒)

MainWidget::~MainWidget()
{
    try {
        // 1. 原子操作设置线程结束标志
        m_isEndThread.store(0, std::memory_order_release);
        
        // 2. 安全停止并清理工作线程
        std::lock_guard<std::mutex> lock(m_threadMutex);
        if (m_loadImagethread && m_loadImagethread->isRunning()) {
            // 记录停止尝试时间
            qDebug() << "Attempting to stop OCR thread at" << QDateTime::currentDateTime().toString();
            
            // 请求线程安全退出
            m_loadImagethread->requestInterruption();
            
            // 等待线程结束
            if (!m_loadImagethread->wait(THREAD_TIMEOUT_MS)) {
                qWarning() << "OCR thread did not finish within" << THREAD_TIMEOUT_MS 
                          << "ms, forcing termination at" 
                          << QDateTime::currentDateTime().toString();
                m_loadImagethread->terminate();
                if (!m_loadImagethread->wait(1000)) { // 额外等待1秒确保终止
                    qCritical() << "Failed to terminate OCR thread";
                }
                m_loadImagethread->deleteLater();
                m_loadImagethread = nullptr;
            } else {
                qDebug() << "OCR thread stopped successfully at" 
                        << QDateTime::currentDateTime().toString();
            }
        }
    } catch (const std::exception& e) {
        qCritical() << "Error in destructor:" << e.what();
    }
}

这些改进提高了代码的健壮性、可维护性和安全性,同时提供了更好的错误处理和日志记录。根据实际应用场景,可能还需要进一步调整和优化。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lichaofan2008, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@lichaofan2008
Copy link
Contributor Author

/merge

@deepin-bot deepin-bot bot merged commit f20d3e7 into linuxdeepin:master Sep 23, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants