Signal handler accessing queue data structure (race condition?)

青春壹個敷衍的年華 提交于 2019-12-04 06:48:04

You have several ways to avoid race condition.

  • Use a wait free (atomic) queue for job pointers;
  • use any other kind of queue, but protect it with sigprocmask (in the non-handler code) and with proper sa_mask value in the sigaction call;
  • don't use signal handler at all, use some non-portable system call which allows working with signals in synchronous way: in Linux it is possible with signalfd, not sure about other platforms.

You need to disable signals with sigprocmask() around your critical sections in the non-handler code. This is analogous to device drivers in the kernel disabling interrupts in the user half of the driver while updating structures shared with an interrupt handler.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!