挥发性sig_atomic_t的正确用法 [英] Proper usage of volatile sig_atomic_t

查看:133
本文介绍了挥发性sig_atomic_t的正确用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据<一个href=\"https://www.securecoding.cert.org/confluence/display/sec$c$c/SIG31-C.+Do+not+access+or+modify+shared+objects+in+signal+handlers\">this现场,可以使用一个讯号处理器中键入挥发性sig_atomic_t 的变量。现在的问题是,就比如像下面这样code仍然是原子,因此没有引入竞争条件?

假设我们使用的是多核处理器(修改:在运行多线程程序)。是否挥发性sig_atomic_t 甚至多核系统摆在首位的工作还是应该使用原子&LT; unsigned int类型&GT; 的C ++ 11多核系统上信号处理(修改:在运行多线程程序)?

 挥发性sig_atomic_t一个;静态无效signal_handler(INT SIG,siginfo_t * SI,无效*未使用)
{
  INT B:
  ................
  B = ...;
  A = A | b:
  ................
}


解决方案

除非你的程序是多线程的,信号处理从来没有同时与其他code在你的程序运行,他们肯定永远不会与code同时运行他们已经打断了。您code是很好,只要信号 SIG 被屏蔽了信号处理的时间。

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;
  ................
}

解决方案

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.

这篇关于挥发性sig_atomic_t的正确用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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