处理中断时如何返回主GUI线程?

问题描述:

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

I have a Raspberry Pi 3 running a GUI program written in Qt. I'm using the wiringPi library to set an interrupt that fires when a certain GPIO pin goes low. When this happens, I want a dialog window to appear telling the user that the Pi will shutdown in 10 seconds, during which they have the option to cancel the shutdown.

问题在于,接收中断的函数在新线程中启动,并且Qt不允许在主线程之外使用计时器等.我想知道如何从中断函数传回主线程.该函数不接受任何参数,顺便说一句.

The problem is that the function that receives the interrupt is launched in a new thread, and Qt does not allow timers, etc. to be used outside of the main thread. I would like to know how I can communicate back to the main thread from the interrupt function. The function accepts no arguments, btw.

示例代码:

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();
} 

AFAIU 中断是仅由 Linux内核处理,并且对应用程序代码不直接可见.但是,请注意 unix信号,并阅读信号安全(7)& 高级Linux编程 & 操作系统:三个简单的组件

AFAIU interrupts are only handled by the Linux kernel and are not directly visible to application code. However, be aware of unix signals and read signal(7) & signal-safety(7) & Advanced Linux Programming & Operating Systems : Three Easy Pieces

关于Qt和信号,已记录在案;参见从Unix信号处理程序调用Qt函数

Regarding Qt and signals, it is documented; see Calling Qt Functions From Unix Signal Handlers