【问题标题】:How to get back to main GUI thread when handling an interrupt?处理中断时如何返回主 GUI 线程?
【发布时间】:2017-06-27 19:38:19
【问题描述】:

我有一个运行用 Qt 编写的 GUI 程序的 Raspberry Pi 3。我正在使用wiringPi library 设置一个在某个 GPIO 引脚变低时触发的中断。发生这种情况时,我希望出现一个对话框窗口,告诉用户 Pi 将在 10 秒内关闭,在此期间他们可以选择取消关闭。

问题是接收中断的函数在新线程中启动,Qt不允许在主线程之外使用定时器等。我想知道如何从中断函数与主线程进行通信。顺便说一句,该函数不接受任何参数。

示例代码:

MainWindow::MainWindow() {
    wiringPiSetup();
    //Set up an interrupt to detect when WiringPI pin 0 (header #11) goes low
    //Call the ShutdownISR function when this happens.
    wiringPiISR(0, INT_EDGE_FALLING, &ShutdownISR);
}

//Non-member, free function. Handles interrupt.
void ShutdownISR() {
    //Crashes the program with errors about doing GUI stuff outside the main thread
    ShutdownDialog* sdDlg = new ShutdownDialog();
    sdDlg->exec();
} 

【问题讨论】:

    标签: c++ multithreading qt raspberry-pi wiringpi


    【解决方案1】:

    AFAIU interrupts 仅由 Linux kernel 处理,对应用程序代码不直接可见。但是,请注意unix signals 并阅读signal(7) & signal-safety(7) & Advanced Linux Programming & Operating Systems : Three Easy Pieces

    关于 Qt 和信号,有文档说明;见Calling Qt Functions From Unix Signal Handlers

    【讨论】:

    • Qt 文档中的示例运行良好。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多