Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
System V msgrcv线程阻塞_C_Multithreading_Pthreads_Signals_Message Queue - Fatal编程技术网

System V msgrcv线程阻塞

System V msgrcv线程阻塞,c,multithreading,pthreads,signals,message-queue,C,Multithreading,Pthreads,Signals,Message Queue,我有一个应用程序,它有信号处理程序,并创建了一个线程来处理消息队列。 下面是信号处理器 /*! \Register handle on SIGINT. */ signal(SIGINT, CloseHandler); VOID CloseHandler(INT32 sig) { if(sig == SIGINT) gAppExitFlag = 1; return; } 我已经创建了一个可连接线程来接收消息队列 /* Initialize and set thread

我有一个应用程序,它有信号处理程序,并创建了一个线程来处理消息队列。 下面是信号处理器

   /*! \Register handle on SIGINT. */
   signal(SIGINT, CloseHandler);

VOID CloseHandler(INT32 sig)
{
if(sig == SIGINT)
gAppExitFlag = 1;
return;
}
我已经创建了一个可连接线程来接收消息队列

      /* Initialize and set thread detached attribute */
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  if(0 != (count = pthread_create(&ModemDetectionHandel, &attr, &ModemDetectionOperation, (void *)&gAppContext)))
  {
     DS3_ERROR(DS3_TELEMETRY_APP, "Error in creating thread ModemDetectionOperation");
  }
在线程中,我创建了一个消息队列,并使用msgflg 0调用msgrcv。 因此,它将阻止,直到收到任何消息。 现在,当我将SIGINT发送到进程,但msgrcv没有返回,线程阻塞在msgrcv中时

我的应用程序在连接线程时卡住了

根据msgrcv手册页*调用进程捕获信号。在这种情况下,系统调用失败,errno设置为EINTR。msgrcv在被中断后不会自动重新启动 信号处理程序,无论建立信号处理程序时SA_重新启动标志的设置如何

为什么线程/msgrcv没有得到信号? 如果我将线程作为主循环,那么它将返回并成功退出应用程序。

如果向进程发送信号,一个未阻止它的线程将处理它,但未指定这将是哪个线程。您说过您没有在所有其他线程上阻止SIGINT。因此,对于您看到的行为,最可能的解释是信号由主线程处理,而不是由调用msgrcv的线程处理

要解决此问题,可以在所有线程上阻止SIGINT,调用msgrcv的线程除外。这将确保信号由该线程处理


如果您有多个线程需要中断,这将不起作用。在这种情况下,您可能需要为所有线程阻塞SIGINT,并在一个特殊的中断处理线程中使用sigwait来接收SIGINT信号。然后,该线程可以向需要中断的所有线程发送另一个信号(可能是SIGUSR1),每个线程有一个pthread_kill。或者也可以使用pthread_cancel,具体取决于您的需要。

如何发送SIGINT信号?你有没有换过信号罩?没有,我没有换信号罩信号罩怎么办?如何生成它?只需执行kill-2命令,然后我还可以在加入线程之前调用pthread_kill。实际上,我的多线程应用程序大约有11-12个线程。当然,如果这对您有效,但我认为处理线程提前退出的情况并不简单。Florian,在我的应用程序中,其他线程正在使用SIGALRM。用这个msgrcv给出了EINTR错误。有什么解决办法吗?我是否可以将线程设置为仅使用pthread_sigmask接收特定信号?是的,您可以使用pthread_sigmask阻止SIGALRM。但是线程将不再能够使用sleep,您将不得不使用nanosleep或usleep函数?也许是时候发布一个新问题了。