Proper usage of volatile sig_atomic_t

最后都变了- 提交于 2019-11-26 20:39:30

问题


According to this site, one can use variables of type volatile sig_atomic_t inside a signal handler. Now my question is, would for example something like the following code still be atomic and thus introduce no race conditions?

Assume that we are using a multicore processor (EDIT: running a multithreaded program). Does volatile sig_atomic_t even work for multicore systems in the first place or should we use the atomic<unsigned int> of C++11 for signal handlers on a multicore system (EDIT: running a multithreaded program)?

volatile sig_atomic_t a;

static void signal_handler(int sig, siginfo_t *si, void *unused)
{
  int b;
  ................
  b = ...;
  a = a | b;
  ................
}

回答1:


Unless your program is multithreaded, signal handlers never run concurrently with other code in your program, and they certainly never run concurrently with the code they've interrupted. Your code is fine as long as the signal sig is masked for the duration of the signal handler.



来源:https://stackoverflow.com/questions/8488791/proper-usage-of-volatile-sig-atomic-t

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