为什么WiringPiISR在启动时不阻止主例程? [英] Why does not WiringPiISR block the main routine when it is fired?

查看:323
本文介绍了为什么WiringPiISR在启动时不阻止主例程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用C语言在树莓板上运行的程序。在程序中,我想使用WiringPiISR处理引脚触发的中断。但是,我发现而不是阻塞主例程,而是用connectionPiISR创建并发线程。我缺少什么吗?

I am developing a program in C to run on a raspberry board. In the program i want to use WiringPiISR to handle a pin triggered interrupt. However, I have found that instead of blocking the main routine, the wiringPiISR creates a concurrent thread. Am i missing something?

最小代码示例:

#include <WiringPi.h>
#include <unistd.h>
int Check=0;
void Isr()
{
    while (1)
    {
       sleep(1);
       Check++;
    }
}

int main()
{
     wiringPiSetup () ;
     pinMode(7, INPUT) ;
     wiringPiISR (7, INT_EDGE_BOTH ,&Isr);
     while (1)
      {
         sleep(2);
         printf("check : %d", Check );
      }
   return 0;
}

我希望这个最小的程序在中断被触发后再也不会恢复在我的情况下,它不断增加变量检查并在屏幕上打印它(两个线程同时工作)。

I would expect this minimal program to never resume after the interrupt is fired but in my case it kept on incrementing the variable check and printing it on the screen ( both threads working concurrently).

推荐答案

href = http://wiringpi.com/reference/priority-interrupts-and-threads/ rel = nofollow noreferrer>文档我发现它很具体(强调我的意思):

The documentation I've found is rather specific (emphasis mine):


intconnectionPiISR(int pin,int edgeType,void(* function)(void));

int wiringPiISR (int pin, int edgeType, void (*function)(void)) ;

此函数在高优先级(如果该程序使用sudo或以root身份运行),并且与主程序同时执行。它具有对所有全局变量,打开文件句柄等的完全访问权限。

This function is run at a high priority (if the program is run using sudo, or as root) and executes concurrently with the main program. It has full access to all the global variables, open file handles and so on.

来源毫无疑问。只是创建了一个新线程:

The sources don't leave anything to imagination. It just crates a new thread:

pthread_create (&threadId, NULL, interruptHandler, &pin) ;

等待中断并执行处理程序:

that waits for interrupt and executes your handler:

static void *interruptHandler (void *arg)
{
  int pin = *(int *)arg ;

  (void)piHiPri (55) ;

  for (;;)
  {
    if (waitForInterrupt (pin, -1) > 0)
      isrFunctions [pin] () ;
  }

  return NULL ;
}

因此,您的处理程序作为单独的线程运行,并且您的行为正常。

So your handler runs as a separate thread and your behavior is expected.

这篇关于为什么WiringPiISR在启动时不阻止主例程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆