我的程序需要对 vector<string>
的内容执行只读访问在 SIGINT
的信号处理程序中. (另一种方法是使用固定长度的 C 字符串的固定大小数组。)该程序设计为在 POSIX 环境中运行。
是vector::operator[]
和 vector::size()
异步安全(或信号安全)?
最佳答案
不,这不安全。 C++11 1.9/6:
When the processing of the abstract machine is interrupted by receipt of a signal, the values of objects which are neither
- of type
volatile std::sig_atomic_t
nor- lock-free atomic objects (29.4)
are unspecified during the execution of the signal handler, and the value of any object not in either of these two categories that is modified by the handler becomes undefined.
关于c++ - 对 vector (vector::operator[] 和 vector::size())的只读访问是异步安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26242231/